Tracks
/
F#
F#
/
Syllabus
/
Datetimes
Da

Datetimes in F#

3 exercises

About Datetimes

A DateTime in F# is an immutable object that contains both date and time information. The date and time information can be accessed through its built-in properties.

Manipulating a DateTime can be done by calling one of its methods. As DateTime values can never change after having been defined, all methods that appear to modify a DateTime will actually return a new DateTime.

Comparing DateTime instances can be done using the default comparison operators (<, >, etc.). The current date (and time) can be retrieved through the DateTime.Now property.

An important aspect of dates in F# is that they are culture-dependent. As such, any DateTime method that deals with strings will be dependent on the current culture. This includes the DateTime.Parse() method that parses a string to a DateTime, as well as the DateTime class' ToString() method that converts a DateTime to a string.

When using a DateTime in a sprintf call, instead of manually converting the DateTime to a string and using the %s string placeholder, one can use the %O placeholder, which will automatically convert the DateTime to its string value.

Edit via GitHub The link opens in a new window or tab

Learn Datetimes

Practicing is locked

Unlock 2 more exercises to practice Datetimes