F# dates and times
last modified December 3, 2026
In this article we work with dates and times in F#.
F# uses the date and time types of .NET. DateTime holds a
date with a time of day, DateOnly and TimeOnly
hold just one of the two, TimeSpan is a length of time, and
DateTimeOffset is a moment together with its offset from UTC.
All of them are immutable: methods such as AddDays return a
new value.
The current date and time
DateTime.Now is the current local time,
DateTime.UtcNow the current time in UTC, and
DateTime.Today today's date at midnight.
open System let now = DateTime.Now printfn "now: %O" now printfn "utc: %O" DateTime.UtcNow printfn "today: %O" DateTime.Today printfn "kind: %A %A" now.Kind DateTime.UtcNow.Kind
The script prints the current time in three ways.
printfn "now: %O" now
%O calls ToString, which uses the culture of the
computer, here en-US. The output of this script is different
on every run; the run below was made in Central Europe, one hour ahead of
UTC.
printfn "kind: %A %A" now.Kind DateTime.UtcNow.Kind
The Kind property records whether a DateTime is
local or UTC. Values that we create ourselves have the kind
Unspecified.
λ dotnet fsi now.fsx now: 12/3/2026 10:03:54 PM utc: 12/3/2026 9:03:54 PM today: 12/3/2026 12:00:00 AM kind: Local Utc
Creating dates
A DateTime is created from its parts: year, month, day and
optionally hours, minutes and seconds. Its properties return the parts and
facts such as the day of the week.
open System let launch = DateTime(2026, 12, 3, 14, 30, 0) printfn "%d-%02d-%02d %02d:%02d" launch.Year launch.Month launch.Day launch.Hour launch.Minute printfn "day of week: %A" launch.DayOfWeek printfn "day of year: %d" launch.DayOfYear printfn "date only: %O" launch.Date printfn "leap years: %b %b" (DateTime.IsLeapYear 2026) (DateTime.IsLeapYear 2028) printfn "days in February 2028: %d" (DateTime.DaysInMonth(2028, 2))
We create a date with a time and print its parts.
printfn "day of week: %A" launch.DayOfWeek
DayOfWeek is an enum with
the cases Sunday to Saturday. The
Date property returns the same date at midnight. An invalid
date, such as DateTime(2026, 2, 30), throws an
ArgumentOutOfRangeException.
λ dotnet fsi create.fsx 2026-12-03 14:30 day of week: Thursday day of year: 337 date only: 12/3/2026 12:00:00 AM leap years: false true days in February 2028: 29
DateOnly and TimeOnly
Birthdays and holidays have no time of day, and opening hours have no
date. DateOnly and TimeOnly describe them
without a meaningless midnight or a dummy date.
open System let birthday = DateOnly(1994, 7, 21) let opens = TimeOnly(8, 30) let closes = TimeOnly(17, 0) printfn "birthday: %s, %A" (birthday.ToString "yyyy-MM-dd") birthday.DayOfWeek printfn "open %O to %O" opens closes let visit = TimeOnly(12, 15) printfn "open at %O: %b" visit (visit.IsBetween(opens, closes)) printfn "open for: %O" (closes - opens) let meeting = birthday.ToDateTime(TimeOnly(10, 0)) printfn "%O" meeting printfn "%O" (DateOnly.FromDateTime(DateTime(2026, 12, 3, 14, 30, 0)))
We work with a date of birth and opening hours.
printfn "open for: %O" (closes - opens)
Subtracting two times gives a TimeSpan, printed as hours,
minutes and seconds. IsBetween checks whether a time falls
into a range.
let meeting = birthday.ToDateTime(TimeOnly(10, 0))
ToDateTime combines a date with a time, and
DateOnly.FromDateTime drops the time from a
DateTime.
λ dotnet fsi dateonly.fsx birthday: 1994-07-21, Thursday open 8:30 AM to 5:00 PM open at 12:15 PM: true open for: 08:30:00 7/21/1994 10:00:00 AM 12/3/2026
Formatting
ToString accepts a format. Custom formats are built from
letters such as yyyy for the year, MM for the
month, dd for the day, HH for 24-hour hours and
mm for minutes. Standard formats are single letters, such as
d for a short date and D for a long date; their
result depends on the culture.
open System
open System.Globalization
let d = DateTime(2026, 12, 3, 14, 30, 0)
let inv = CultureInfo.InvariantCulture
printfn "%s" (d.ToString("yyyy-MM-dd HH:mm", inv))
printfn "%s" (d.ToString("dd.MM.yyyy h:mm tt", inv))
printfn "%s" (d.ToString("dddd, MMMM d", inv))
printfn "%s" (d.ToString("o", inv))
printfn "%s" (d.ToString("yyyy-MM-dd'T'HH:mm:ss", inv))
for name in [ "en-US"; "de-DE"; "sk-SK" ] do
let c = CultureInfo name
printfn "%-6s %-10s %s" name (d.ToString("d", c)) (d.ToString("D", c))
printfn $"{d:ddd} {d:yyyyMMdd}"
printfn $"""{d.ToString "yyyy-MM-dd"}"""
One moment is formatted with custom formats and with the short and long date formats of three cultures.
printfn "%s" (d.ToString("dd.MM.yyyy h:mm tt", inv))
Letter case matters: MM is the month, mm the
minutes, HH hours from 0 to 23, h hours from 1
to 12, and tt the AM or PM mark. The o format
gives the ISO 8601 round-trip form with fractions of a second; quotes
insert literal text such as the T.
printfn $"{d:ddd} {d:yyyyMMdd}"
printfn $"""{d.ToString "yyyy-MM-dd"}"""
An interpolated string takes
the format after a colon and uses the current culture. In F# 11 the format
may contain only letters and digits; a format with -,
., /, : or a space is error FS0010.
For such formats we call ToString inside the hole. The
triple-quoted string lets the hole contain ordinary quotes.
λ dotnet fsi formats.fsx 2026-12-03 14:30 03.12.2026 2:30 PM Thursday, December 3 2026-12-03T14:30:00.0000000 2026-12-03T14:30:00 en-US 12/3/2026 Thursday, December 3, 2026 de-DE 03.12.2026 Donnerstag, 3. Dezember 2026 sk-SK 3. 12. 2026 štvrtok 3. decembra 2026 Thu 20261203 2026-12-03
Parsing dates
DateTime.Parse guesses the format from the culture, which
makes it unreliable for data: 03/12/2026 is March 12 in the
US and December 3 in most of Europe. ParseExact and
TryParseExact take the expected format.
open System
open System.Globalization
let inv = CultureInfo.InvariantCulture
let tryDate (text: string) =
match DateOnly.TryParseExact(text, "yyyy-MM-dd", inv, DateTimeStyles.None) with
| true, d -> Some d
| false, _ -> None
for text in [ "2026-12-03"; "2026-02-30"; "3.12.2026"; "2026-1-5" ] do
match tryDate text with
| Some d -> printfn "%-10s -> %A" text d.DayOfWeek
| None -> printfn "%-10s -> invalid" text
let formats = [| "dd.MM.yyyy"; "d.M.yyyy"; "yyyy-MM-dd" |]
let stamp = DateTime.ParseExact("3.12.2026", formats, inv, DateTimeStyles.None)
printfn "%s" (stamp.ToString("yyyy-MM-dd", inv))
printfn "en-US month: %d" (DateTime.Parse("03/12/2026", CultureInfo "en-US")).Month
printfn "de-DE month: %d" (DateTime.Parse("03/12/2026", CultureInfo "de-DE")).Month
Several strings are checked against one format, and one string is parsed with a list of allowed formats.
match DateOnly.TryParseExact(text, "yyyy-MM-dd", inv, DateTimeStyles.None) with
The text must match the format exactly. 2026-1-5 fails
because MM needs two digits, and 2026-02-30 has
the right form but is not a real date.
let stamp = DateTime.ParseExact("3.12.2026", formats, inv, DateTimeStyles.None)
With an array of formats, the first one that matches is used, which helps when the data comes in a few known variants.
λ dotnet fsi parsing.fsx 2026-12-03 -> Thursday 2026-02-30 -> invalid 3.12.2026 -> invalid 2026-1-5 -> invalid 2026-12-03 en-US month: 3 de-DE month: 12
Date arithmetic
The Add methods move a date forward or back. Subtracting two
dates gives the TimeSpan between them.
open System let show (d: DateTime) = d.ToString "yyyy-MM-dd" let start = DateTime(2026, 1, 31) printfn "+10 days: %s" (show (start.AddDays 10.0)) printfn "-10 days: %s" (show (start.AddDays -10.0)) printfn "+1 month: %s" (show (start.AddMonths 1)) printfn "+2 months: %s" (show (start.AddMonths 2)) let leapDay = DateTime(2028, 2, 29) printfn "+1 year: %s" (show (leapDay.AddYears 1)) let holiday = DateTime(2026, 12, 24) let today = DateTime(2026, 12, 3) let left = holiday - today printfn "days to holiday: %d (%.0f weeks)" left.Days (left.TotalDays / 7.0) let birth = DateOnly(1994, 7, 21) let now = DateOnly(2026, 12, 3) printfn "days lived: %d" (now.DayNumber - birth.DayNumber)
We add days, months and years and count the days between dates.
printfn "+1 month: %s" (show (start.AddMonths 1))
January 31 plus one month would be February 31, which does not exist, so
AddMonths moves back to the last day of February. Adding two
months gives March 31. In the same way, a year after February 29 is
February 28.
printfn "days lived: %d" (now.DayNumber - birth.DayNumber)
DateOnly values cannot be subtracted directly.
DayNumber counts days from January 1 of year 1, so the
difference of two day numbers is the number of days between them.
λ dotnet fsi arithmetic.fsx +10 days: 2026-02-10 -10 days: 2026-01-21 +1 month: 2026-02-28 +2 months: 2026-03-31 +1 year: 2029-02-28 days to holiday: 21 (3 weeks) days lived: 11823
TimeSpan
A TimeSpan is a duration. It is created with methods such as
TimeSpan.FromMinutes or by subtracting times, and it can be
added, compared and summed.
open System
let laps =
[ TimeSpan.FromSeconds 312.4
TimeSpan(0, 5, 3)
TimeSpan.FromMinutes 4.75 ]
let total = List.reduce (+) laps
let best = List.min laps
printfn "total: %O" total
printfn "total minutes: %.2f" total.TotalMinutes
printfn "parts: %d min %d s %d ms" total.Minutes total.Seconds total.Milliseconds
printfn "best: %s" (best.ToString @"m\:ss")
let average = total / float laps.Length
printfn "average: %s" (average.ToString @"m\:ss\.f")
let shift = TimeSpan.FromHours 7.5 + TimeSpan.FromMinutes 45.0
printfn "shift: %O, longer than 8 h: %b" shift (shift > TimeSpan.FromHours 8.0)
We add up three lap times of a run.
printfn "parts: %d min %d s %d ms" total.Minutes total.Seconds total.Milliseconds
Minutes and Seconds are the parts of the
duration, while TotalMinutes is the whole duration expressed
in minutes as a float.
printfn "best: %s" (best.ToString @"m\:ss")
In a custom TimeSpan format, separators such as the colon
must be escaped with a backslash, so the format is written as a verbatim
string. List.min works because TimeSpan supports
comparison. List.sum does not: it needs a Zero
member, which TimeSpan lacks (error FS0001), so the laps are
added with List.reduce (+).
λ dotnet fsi durations.fsx total: 00:15:00.4000000 total minutes: 15.01 parts: 15 min 0 s 400 ms best: 4:45 average: 5:00.1 shift: 08:15:00, longer than 8 h: true
Comparing and sorting
Dates support the comparison operators, so they work with
max, List.sortBy and the other functions that
compare values.
open System
type Task = { Name: string; Due: DateOnly }
let today = DateOnly(2026, 12, 3)
let tasks =
[ { Name = "Renew passport"; Due = DateOnly(2027, 1, 15) }
{ Name = "Pay rent"; Due = DateOnly(2026, 12, 1) }
{ Name = "Book flights"; Due = DateOnly(2026, 12, 10) }
{ Name = "Send cards"; Due = DateOnly(2026, 12, 3) } ]
for t in tasks |> List.sortBy (fun t -> t.Due) do
let days = t.Due.DayNumber - today.DayNumber
let status =
if days < 0 then $"overdue by {-days} days"
elif days = 0 then "due today"
else $"in {days} days"
printfn "%s %-15s %s" (t.Due.ToString "yyyy-MM-dd") t.Name status
let last = tasks |> List.maxBy (fun t -> t.Due)
printfn "last deadline: %s" last.Name
The task list is sorted by due date, and each task is compared with today.
for t in tasks |> List.sortBy (fun t -> t.Due) do
The comparison is chronological. today is a fixed date here
so that the output stays the same; a real program would use
DateOnly.FromDateTime DateTime.Today.
λ dotnet fsi compare.fsx 2026-12-01 Pay rent overdue by 2 days 2026-12-03 Send cards due today 2026-12-10 Book flights in 7 days 2027-01-15 Renew passport in 43 days last deadline: Renew passport
Time zones
A DateTime does not say where it was measured.
DateTimeOffset stores the offset from UTC as well, so it
identifies one moment on Earth. TimeZoneInfo converts moments
between time zones and knows their daylight saving time rules.
open System
let call = DateTimeOffset(2026, 12, 3, 15, 0, 0, TimeSpan.Zero)
let zones = [ "Europe/Bratislava"; "America/New_York"; "Asia/Tokyo"; "Australia/Sydney" ]
for id in zones do
let zone = TimeZoneInfo.FindSystemTimeZoneById id
let local = TimeZoneInfo.ConvertTime(call, zone)
printfn "%-18s %s" id (local.ToString "yyyy-MM-dd HH:mm zzz")
let bratislava = TimeZoneInfo.FindSystemTimeZoneById "Europe/Bratislava"
let summer = DateTime(2026, 7, 1)
let winter = DateTime(2026, 12, 1)
printfn "offset in July: %O, in December: %O" (bratislava.GetUtcOffset summer) (bratislava.GetUtcOffset winter)
let a = DateTimeOffset(2026, 12, 3, 16, 0, 0, TimeSpan.FromHours 1.0)
printfn "same moment: %b" (a = call)
A video call at 15:00 UTC is shown in four time zones.
let local = TimeZoneInfo.ConvertTime(call, zone)
The zone identifiers come from the IANA time zone database, which Linux and macOS use; recent .NET versions accept them on Windows too. In December, Sydney is on summer time, eleven hours ahead of UTC, and its date is already the next day.
printfn "same moment: %b" (a = call)
16:00 at UTC+1 is the same moment as 15:00 UTC, and
DateTimeOffset values compare by the moment, not by the
digits. Storing times in UTC or as DateTimeOffset avoids
confusion when a program runs in several time zones.
λ dotnet fsi zones.fsx Europe/Bratislava 2026-12-03 16:00 +01:00 America/New_York 2026-12-03 10:00 -05:00 Asia/Tokyo 2026-12-04 00:00 +09:00 Australia/Sydney 2026-12-04 02:00 +11:00 offset in July: 02:00:00, in December: 01:00:00 same moment: true
Counting working days
The last program computes a deadline in working days. It skips weekends and public holidays, and counts the working days in a month.
open System
let holidays =
set [ DateOnly(2026, 12, 24); DateOnly(2026, 12, 25); DateOnly(2026, 12, 26)
DateOnly(2027, 1, 1); DateOnly(2027, 1, 6) ]
let isWorkday (d: DateOnly) =
d.DayOfWeek <> DayOfWeek.Saturday
&& d.DayOfWeek <> DayOfWeek.Sunday
&& not (holidays.Contains d)
let addWorkdays (n: int) (start: DateOnly) =
let rec loop (d: DateOnly) left =
if left = 0 then d
else
let next = d.AddDays 1
loop next (if isWorkday next then left - 1 else left)
loop start n
let workdaysIn year month =
[ 1 .. DateTime.DaysInMonth(year, month) ]
|> List.filter (fun day -> isWorkday (DateOnly(year, month, day)))
|> List.length
let fmt (d: DateOnly) = d.ToString "ddd yyyy-MM-dd"
let ordered = DateOnly(2026, 12, 3)
for n in [ 5; 15; 25 ] do
printfn "%2d working days after %s: %s" n (fmt ordered) (fmt (addWorkdays n ordered))
printfn "working days in December 2026: %d" (workdaysIn 2026 12)
printfn "working days in January 2027: %d" (workdaysIn 2027 1)
An order placed on December 3 must be delivered within a number of working days. The holidays are a short example list for the turn of the year; a real program would load them from a file.
let rec loop (d: DateOnly) left =
if left = 0 then d
else
let next = d.AddDays 1
loop next (if isWorkday next then left - 1 else left)
The recursive function moves one day at a
time and counts down only on working days. The holidays are kept in a
set, which makes the lookup fast and works
because DateOnly values can be compared.
let fmt (d: DateOnly) = d.ToString "ddd yyyy-MM-dd"
ddd adds the abbreviated name of the day, so it is easy to
check that no deadline falls on a weekend.
λ dotnet fsi workdays.fsx 5 working days after Thu 2026-12-03: Thu 2026-12-10 15 working days after Thu 2026-12-03: Mon 2026-12-28 25 working days after Thu 2026-12-03: Wed 2027-01-13 working days in December 2026: 21 working days in January 2027: 19
Source
.NET documentation: Dates, times, and time zones
.NET documentation: Custom date and time format strings
In this article we have read the current time, created dates and taken
them apart, used DateOnly and TimeOnly, formatted
and parsed dates, added days and months, worked with
TimeSpan, sorted dates, converted between time zones and
counted working days. Format strings in interpolated strings are covered in
F# string interpolation, and
parsing other values in F# type
conversion.
Author
List all F# tutorials.