Working with Dates
Documentation for WebSharper Date utilities (System.DateTime and DateFNS)
Dates in WebSharper
WebSharper supports date and time manipulation through two complementary approaches:
- Native support via the .NET
System.DateTime
API, fully usable in WebSharper. - Functional, JavaScript-style operations via the
WebSharper.DateFNS
binding, which exposes thedate-fns
library.
Why Use System.DateTime
?
The .NET System.DateTime
type is ideal when you need:
- Strong typing and .NET compatibility
- Server-side consistency
- Access to the full breadth of the standard
DateTime
API
Example usage:
Why Use the DateFNS
?
date-fns
is a popular JavaScript date utility library that provides functional, immutable operations.
With the official WebSharper.DateFNS
binding, you can use its concise APIs directly from F#:
This style is especially convenient in client-side F# where you want short, declarative date code similar to JavaScript.
Note: please make sure you install
date-fns
vianpm install date-fns
Date Input & Interop
Templating with WebSharper.UI
index.html:
(See WebSharper Templating for more.)
HTML Combinator Style
(See WebSharper’s HTML Combinators for more.)
Passing System.DateTime
to DateFNS
You can convert a System.DateTime
to a JS Date using .JS
(common interop pattern):
This lets you mix both systems smoothly.
When to Use Which
Scenario | Use System.DateTime | Use DateFNS (WebSharper binding) |
---|---|---|
Full compatibility with .NET APIs | Native integration | Less direct |
Immutable / functional style | Manual handling required | Built-in pure functions |
Tree-shaking & bundle management | Minimal overhead | Only imported functions included |
Rich client-side date utilities | Basic functionality | Extensive toolkit via date-fns |
Interop between systems | .NET-first design | JS-friendly style |
Quick Decision Flow
- Is this server-side or .NET-heavy logic? → Stick with
System.DateTime
. - Is this client-side UI code where concise, functional style matters? → Prefer
DateFNS
. - Need to intermix both? → You can compute with either and convert between them as needed.