DateOnly and TimeOnly

suggest change

DateTime always holds both a date and a time. Lots of values have only one of them: a birthday, an invoice due date or a public holiday has no time, and a store's opening hour or a daily alarm has no date. Before .NET 6 the usual workaround was a DateTime at midnight for dates and a TimeSpan for times of day. That works until something treats the midnight as a real time.

.NET 6 added two types for this:

Both are small immutable structs (4 and 8 bytes) in the System namespace. Neither knows about time zones.

Versions:

The examples are .NET 10 / C# 14 file-based programs: save the code as app.cs and run dotnet run app.cs. All output comes from real runs.

The problem with DateTime for dates

Here's a birthday kept as a DateTime at midnight. Somewhere along the way it gets converted to UTC, which is a common rule for "store everything as UTC":

// A birthday stored as a DateTime at midnight in Warsaw
var warsaw = TimeZoneInfo.FindSystemTimeZoneById("Europe/Warsaw");
var birthday = new DateTime(1990, 5, 17);

DateTime utc = TimeZoneInfo.ConvertTimeToUtc(birthday, warsaw);
Console.WriteLine($"stored:  {utc:yyyy-MM-dd HH:mm} UTC");
Console.WriteLine($"date:    {utc:yyyy-MM-dd}");

// DateOnly has no time and no time zone to get wrong
var born = new DateOnly(1990, 5, 17);
Console.WriteLine($"DateOnly: {born:yyyy-MM-dd}");
stored:  1990-05-16 22:00 UTC
date:    1990-05-16
DateOnly: 1990-05-17

The person's birthday moved to the previous day. A date isn't a moment in time, so no time zone conversion should apply to it. DateOnly makes that impossible: it has no time part to shift.

Choose the type by what the value means:

Value Type
a moment in time (log entry, order placed) DateTimeOffset or UTC DateTime
a calendar date (birthday, due date, holiday) DateOnly
a time on the wall clock (opens at 9:00, alarm at 6:30) TimeOnly
a duration (took 90 minutes, 25 hours of work) TimeSpan

Creating values

var release = new DateOnly(2026, 11, 10);
var standup = new TimeOnly(9, 30);
var precise = new TimeOnly(23, 59, 59, 999, 999);   // hour, minute, second, ms, µs

// there's no DateOnly.Today or TimeOnly.Now
DateOnly today = DateOnly.FromDateTime(DateTime.Now);
TimeOnly now = TimeOnly.FromDateTime(DateTime.Now);

Console.WriteLine(release.ToString("yyyy-MM-dd"));
Console.WriteLine(standup.ToString("HH:mm"));
Console.WriteLine(precise.ToString("HH:mm:ss.ffffff"));

Console.WriteLine(DateOnly.MinValue.ToString("yyyy-MM-dd"));
Console.WriteLine(DateOnly.MaxValue.ToString("yyyy-MM-dd"));
Console.WriteLine(TimeOnly.MinValue.ToString("HH:mm:ss.fffffff"));
Console.WriteLine(TimeOnly.MaxValue.ToString("HH:mm:ss.fffffff"));

Console.WriteLine(TimeOnly.FromTimeSpan(TimeSpan.FromMinutes(90)).ToString("HH:mm"));
Console.WriteLine(DateOnly.FromDayNumber(0).ToString("yyyy-MM-dd"));

try { var bad = new DateOnly(2026, 2, 29); }
catch (ArgumentOutOfRangeException e) { Console.WriteLine(e.Message); }
try { var bad = new TimeOnly(24, 0); }
catch (ArgumentOutOfRangeException e) { Console.WriteLine(e.Message); }
2026-11-10
09:30
23:59:59.999999
0001-01-01
9999-12-31
00:00:00.0000000
23:59:59.9999999
01:30
0001-01-01
Year, Month, and Day parameters describe an un-representable DateTime.
Hour, Minute, and Second parameters describe an un-representable DateTime.

default(DateOnly) is DateOnly.MinValue (0001-01-01) and default(TimeOnly) is midnight. TimeOnly has no 24:00. A range that ends at midnight wraps around, as shown later.

"Today" depends on the time zone, and DateTime.Now uses the machine's. On a server, compute today in the user's or business's zone, as shown in the section on converting to a moment in time below. To make "today" testable, take the time from a TimeProvider instead of DateTime.Now; the "TimeProvider and PeriodicTimer" article covers that.

Properties and DayNumber

var d = new DateOnly(2026, 10, 18);
Console.WriteLine($"{d.Year} {d.Month} {d.Day}");
Console.WriteLine(d.DayOfWeek);
Console.WriteLine(d.DayOfYear);
Console.WriteLine(d.DayNumber);

var t = new TimeOnly(14, 5, 30, 250, 125);
Console.WriteLine($"{t.Hour} {t.Minute} {t.Second} {t.Millisecond} {t.Microsecond} {t.Nanosecond}");
Console.WriteLine(t.Ticks);

// Deconstruct (.NET 8+)
var (year, month, day) = d;
var (hour, minute, second) = t;
Console.WriteLine($"{year}/{month}/{day} {hour}h{minute}m{second}s");
2026 10 18
Sunday
291
739906
14 5 30 250 125 0
507302501250
2026/10/18 14h5m30s

DayNumber is the number of days since 0001-01-01. Internally that's all a DateOnly stores, which is why it's 4 bytes. DateOnly.FromDayNumber converts back. Day numbers are handy for date differences and for storing a date as an integer.

TimeOnly.Ticks counts 100-nanosecond ticks since midnight. TimeOnly can't go below one tick, so Nanosecond is always a multiple of 100.

Destructuring covers the Deconstruct methods along with other built-in ones.

Formatting

ToString() without arguments uses the current culture's short date or short time pattern, the same as DateTime:

using System.Globalization;

var d = new DateOnly(2026, 10, 18);
var t = new TimeOnly(14, 30, 15);

foreach (var name in new[] { "en-US", "de-DE", "ja-JP", "fr-FR" })
{
    var c = CultureInfo.GetCultureInfo(name);
    Console.WriteLine($"{name}: {d.ToString(c)} | {d.ToString("D", c)} | {t.ToString(c)}");
}

var inv = CultureInfo.InvariantCulture;
Console.WriteLine(d.ToString("o", inv));
Console.WriteLine(t.ToString("o", inv));
Console.WriteLine(d.ToString("R", inv));
Console.WriteLine(t.ToString("R", inv));
Console.WriteLine(d.ToString("ddd, d MMM yyyy", inv) + " at " + t.ToString("h:mm tt", inv));
Console.WriteLine(string.Create(inv, $"{d:yyyy-MM-dd}T{t:HH:mm}"));
en-US: 10/18/2026 | Sunday, October 18, 2026 | 2:30 PM
de-DE: 18.10.2026 | Sonntag, 18. Oktober 2026 | 14:30
ja-JP: 2026/10/18 | 2026年10月18日日曜日 | 14:30
fr-FR: 18/10/2026 | dimanche 18 octobre 2026 | 14:30
2026-10-18
14:30:15.0000000
Sun, 18 Oct 2026
14:30:15
Sun, 18 Oct 2026 at 2:30 PM
2026-10-18T14:30

The custom format specifiers are the ones from DateTime Formatting, restricted to the parts the type has. "o" (round-trip) gives ISO 8601: yyyy-MM-dd for DateOnly and HH:mm:ss.fffffff for TimeOnly. Use "o" or an explicit invariant format for anything a program reads back, such as file names, logs, URLs and CSV. Culture-dependent output is for people.

A specifier for a part the type doesn't have, like HH on a DateOnly or yyyy on a TimeOnly, throws FormatException.

ToLongDateString(), ToShortDateString(), ToLongTimeString() and ToShortTimeString() also exist, and use the current culture.

Parsing

Parse, TryParse, ParseExact and TryParseExact work like their DateTime versions:

using System.Globalization;

var inv = CultureInfo.InvariantCulture;
CultureInfo.CurrentCulture = inv;

Console.WriteLine(DateOnly.Parse("2026-10-18", inv));
Console.WriteLine(DateOnly.Parse("18.10.2026", CultureInfo.GetCultureInfo("de-DE")).ToString("o"));
Console.WriteLine(DateOnly.ParseExact("18/10/2026", "dd/MM/yyyy", inv).ToString("o"));
Console.WriteLine(TimeOnly.Parse("14:30", inv));
Console.WriteLine(TimeOnly.Parse("2:30 PM", inv).ToString("HH:mm"));
Console.WriteLine(TimeOnly.ParseExact("0930", "HHmm", inv));

// a time part makes DateOnly parsing fail
try { DateOnly.Parse("2026-10-18 14:30", inv); }
catch (FormatException e) { Console.WriteLine(e.Message); }

foreach (var s in new[] { "2026-02-28", "2026-02-29", "yesterday" })
{
    if (DateOnly.TryParseExact(s, "yyyy-MM-dd", inv, DateTimeStyles.None, out var date))
        Console.WriteLine($"{s} -> {date.DayOfWeek}");
    else
        Console.WriteLine($"{s} -> invalid");
}
10/18/2026
2026-10-18
2026-10-18
14:30
14:30
09:30
String '2026-10-18 14:30' contains parts which are not specific to the DateOnly.
2026-02-28 -> Saturday
2026-02-29 -> invalid
yesterday -> invalid

The first line shows that the invariant culture's short date pattern is US-style MM/dd/yyyy. Don't rely on it for machine-readable output.

DateOnly parsing is strict: if the text has a time, even midnight, it fails. Parse with DateTime.Parse and call DateOnly.FromDateTime if your input contains timestamps and you only want the date.

For user input, prefer TryParseExact with the formats you accept. Parse and TryParse with culture info covers culture issues, and they apply here too.

Since .NET 7 both types implement IParsable<T> and ISpanParsable<T>, so they work with generic parsing code (see Static abstract interface members). They also work wherever ASP.NET Core and System.CommandLine bind values: a DateOnly route or query parameter, or an Option<DateOnly>, needs no extra code.

Converting to and from DateTime

var dt = new DateTime(2026, 10, 18, 14, 30, 0, DateTimeKind.Utc);

DateOnly date = DateOnly.FromDateTime(dt);
TimeOnly time = TimeOnly.FromDateTime(dt);
Console.WriteLine($"{date:o} {time:o}");

// back to DateTime: midnight, or a given time, with an optional Kind
DateTime midnight = date.ToDateTime(TimeOnly.MinValue);
DateTime local = date.ToDateTime(new TimeOnly(9, 0));
DateTime utc = date.ToDateTime(new TimeOnly(9, 0), DateTimeKind.Utc);
Console.WriteLine($"{midnight:s} {midnight.Kind}");
Console.WriteLine($"{local:s} {local.Kind}");
Console.WriteLine($"{utc:s} {utc.Kind}");

// TimeOnly <-> TimeSpan
TimeSpan ts = time.ToTimeSpan();
Console.WriteLine(ts);
Console.WriteLine(TimeOnly.FromTimeSpan(ts + TimeSpan.FromMinutes(45)).ToString("HH:mm"));

// TimeSpan of 24h or more doesn't fit
try { TimeOnly.FromTimeSpan(TimeSpan.FromHours(25)); }
catch (ArgumentOutOfRangeException e) { Console.WriteLine(e.GetType().Name); }

// Deconstruct a DateTime (.NET 8+)
var (d2, t2) = dt;
Console.WriteLine($"{d2:o} | {t2:o}");
2026-10-18 14:30:00.0000000
2026-10-18T00:00:00 Unspecified
2026-10-18T09:00:00 Unspecified
2026-10-18T09:00:00 Utc
14:30:00
15:15
ArgumentOutOfRangeException
2026-10-18 | 14:30:00.0000000

FromDateTime just takes the parts. It ignores Kind and doesn't convert time zones. ToDateTime gives a DateTime with Kind = Unspecified unless you pass one.

Converting to a moment in time

A date plus a wall-clock time is still not a moment in time. You need a time zone, and the offset depends on the date because of daylight saving time:

// A meeting on a calendar date at a wall-clock time in a given zone
var date = new DateOnly(2026, 3, 29);
var time = new TimeOnly(9, 0);
var zone = TimeZoneInfo.FindSystemTimeZoneById("Europe/Warsaw");

DateTime wall = date.ToDateTime(time);                 // Kind = Unspecified
TimeSpan offset = zone.GetUtcOffset(wall);
var start = new DateTimeOffset(wall, offset);
Console.WriteLine(start.ToString("o"));
Console.WriteLine(start.UtcDateTime.ToString("o"));

// The day before DST started the offset was +01:00
var dayBefore = date.AddDays(-1).ToDateTime(time);
Console.WriteLine(new DateTimeOffset(dayBefore, zone.GetUtcOffset(dayBefore)).ToString("o"));

// "Today" depends on the time zone
var instant = new DateTimeOffset(2026, 10, 18, 23, 30, 0, TimeSpan.Zero);
var tokyo = TimeZoneInfo.FindSystemTimeZoneById("Asia/Tokyo");
Console.WriteLine(DateOnly.FromDateTime(instant.UtcDateTime).ToString("o"));
Console.WriteLine(DateOnly.FromDateTime(TimeZoneInfo.ConvertTime(instant, tokyo).DateTime).ToString("o"));
2026-03-29T09:00:00.0000000+02:00
2026-03-29T07:00:00.0000000Z
2026-03-28T09:00:00.0000000+01:00
2026-10-18
2026-10-19

Store DateOnly + TimeOnly + time zone ID for future events like "every Monday at 9:00 in Warsaw". Compute the DateTimeOffset when you need it, so a change to the zone's rules doesn't break stored events.

Date arithmetic

DateOnly has AddDays, AddMonths and AddYears, plus the comparison operators:

var d = new DateOnly(2026, 1, 31);

Console.WriteLine(d.AddDays(1).ToString("o"));
Console.WriteLine(d.AddDays(-31).ToString("o"));
Console.WriteLine(d.AddMonths(1).ToString("o"));   // clamped to the last day of February
Console.WriteLine(new DateOnly(2028, 2, 29).AddYears(1).ToString("o"));

// days between two dates
var start = new DateOnly(2026, 10, 18);
var end = new DateOnly(2026, 12, 25);
int days = end.DayNumber - start.DayNumber;
Console.WriteLine($"{days} days until Christmas");

Console.WriteLine(start < end);
Console.WriteLine(start == new DateOnly(2026, 10, 18));
Console.WriteLine(start.CompareTo(end));

DateOnly[] dates = [new(2026, 12, 1), new(2025, 6, 30), new(2026, 1, 15)];
Console.WriteLine(string.Join(", ", dates.Order().Select(x => x.ToString("o"))));
Console.WriteLine(dates.Max().ToString("o"));
2026-02-01
2025-12-31
2026-02-28
2029-02-28
68 days until Christmas
True
True
-1
2025-06-30, 2026-01-15, 2026-12-01
2026-12-01

There's no - operator between two DateOnly values and no Add(TimeSpan):

var a = new DateOnly(2026, 12, 25);
var b = new DateOnly(2026, 10, 18);
var diff = a - b;
app.cs(3,12): error CS0019: Operator '-' cannot be applied to operands of type 'DateOnly' and 'DateOnly'

Subtract DayNumbers to get a number of days. A TimeSpan would suggest hours and minutes that a date doesn't have.

Time-of-day arithmetic wraps around

TimeOnly is a position on a 24-hour clock, so adding and subtracting wrap around midnight instead of throwing:

var late = new TimeOnly(22, 30);

// adding wraps around midnight
Console.WriteLine(late.AddHours(3).ToString("HH:mm"));
Console.WriteLine(late.AddMinutes(-23 * 60).ToString("HH:mm"));

// the out parameter tells you how many days you crossed
TimeOnly t = late.AddHours(50, out int wrapped);
Console.WriteLine($"{t:HH:mm} (+{wrapped} days)");
t = new TimeOnly(1, 0).Add(TimeSpan.FromHours(-3), out wrapped);
Console.WriteLine($"{t:HH:mm} ({wrapped} days)");

// subtracting also wraps: the result is always 0 to 24h
var start = new TimeOnly(22, 0);
var end = new TimeOnly(6, 30);
Console.WriteLine(end - start);
Console.WriteLine(start - end);

// IsBetween handles ranges that cross midnight
var nightShiftStart = new TimeOnly(22, 0);
var nightShiftEnd = new TimeOnly(6, 0);
foreach (var now in new TimeOnly[] { new(23, 15), new(3, 0), new(6, 0), new(12, 0) })
    Console.WriteLine($"{now:HH:mm} night shift: {now.IsBetween(nightShiftStart, nightShiftEnd)}");

Console.WriteLine(new TimeOnly(9, 0).IsBetween(new TimeOnly(9, 0), new TimeOnly(17, 0)));
Console.WriteLine(new TimeOnly(17, 0).IsBetween(new TimeOnly(9, 0), new TimeOnly(17, 0)));
01:30
23:30
00:30 (+3 days)
22:00 (-1 days)
08:30:00
15:30:00
23:15 night shift: True
03:00 night shift: True
06:00 night shift: False
12:00 night shift: False
True
False

Things to know:

The wrap-around also means you should compare TimeOnly values with care. 23:15 > 03:00 is true even though, on a night shift, 03:00 comes later. For real ordering across days, combine with a date.

Practical examples

Age, workdays in a range, and "first Monday of the month":

var today = new DateOnly(2026, 10, 18);

Console.WriteLine(Age(new DateOnly(1990, 5, 17), today));
Console.WriteLine(Age(new DateOnly(2000, 10, 19), today));
Console.WriteLine(Age(new DateOnly(2008, 2, 29), new DateOnly(2026, 2, 28)));

foreach (var d in Days(new DateOnly(2026, 12, 23), new DateOnly(2026, 12, 29)).Where(IsWorkday))
    Console.Write($"{d:ddd dd} ");
Console.WriteLine();

// first Monday of each month in 2027 Q1
for (int m = 1; m <= 3; m++)
{
    var first = new DateOnly(2027, m, 1);
    int shift = ((int)DayOfWeek.Monday - (int)first.DayOfWeek + 7) % 7;
    Console.WriteLine(first.AddDays(shift).ToString("ddd yyyy-MM-dd"));
}

static int Age(DateOnly born, DateOnly on)
{
    int age = on.Year - born.Year;
    if (on < born.AddYears(age)) age--;
    return age;
}

static IEnumerable<DateOnly> Days(DateOnly from, DateOnly to)
{
    for (var d = from; d <= to; d = d.AddDays(1))
        yield return d;
}

static bool IsWorkday(DateOnly d) => d.DayOfWeek is not (DayOfWeek.Saturday or DayOfWeek.Sunday);
36
25
18
Wed 23 Thu 24 Fri 25 Mon 28 Tue 29 
Mon 2027-01-04
Mon 2027-02-01
Mon 2027-03-01

AddYears clamps February 29 to February 28, so someone born on a leap day turns 18 on February 28 in a non-leap year. If your rules say March 1, write that rule explicitly. The C# 14 extension members article turns IsWeekend and NextWorkday into properties on DateOnly.

Opening hours in a store's time zone, including a day that closes after midnight:

var hours = new Dictionary<DayOfWeek, (TimeOnly Open, TimeOnly Close)>
{
    [DayOfWeek.Friday] = (new(10, 0), new(2, 0)),     // closes after midnight
    [DayOfWeek.Saturday] = (new(12, 0), new(23, 0)),
};

var store = TimeZoneInfo.FindSystemTimeZoneById("Europe/Warsaw");
DateTimeOffset[] instants =
[
    new(2026, 10, 16, 20, 0, 0, TimeSpan.Zero),
    new(2026, 10, 17, 1, 30, 0, TimeSpan.Zero),
    new(2026, 10, 17, 10, 30, 0, TimeSpan.Zero),
];

foreach (var instant in instants)
{
    var local = TimeZoneInfo.ConvertTime(instant, store);
    var (day, time) = local.DateTime;
    Console.WriteLine($"{day:ddd} {time:HH:mm} open: {IsOpen(day, time)}");
}

bool IsOpen(DateOnly day, TimeOnly time)
{
    // before the opening time, an after-midnight close belongs to yesterday's hours
    if (hours.TryGetValue(day.AddDays(-1).DayOfWeek, out var y) && y.Close < y.Open && time < y.Close)
        return true;
    return hours.TryGetValue(day.DayOfWeek, out var h) && time >= h.Open
        && (h.Close < h.Open || time < h.Close);
}
Fri 22:00 open: True
Sat 03:30 open: False
Sat 12:30 open: True

The instants are UTC. Converting to the store's zone first and then splitting into DateOnly and TimeOnly keeps the rest of the logic free of time zones.

JSON

Since .NET 7, System.Text.Json reads and writes both types as ISO 8601 strings:

#:property PublishAot=false
using System.Text.Json;

var shift = new Shift("Ana", new DateOnly(2026, 10, 18), new TimeOnly(22, 0), new TimeOnly(6, 30, 15, 500));
string json = JsonSerializer.Serialize(shift);
Console.WriteLine(json);

var back = JsonSerializer.Deserialize<Shift>(json)!;
Console.WriteLine(back == shift);

var parsed = JsonSerializer.Deserialize<Shift>("""{"Name":"Bo","Day":"2026-10-19","Start":"08:00","End":"16:00:00"}""")!;
Console.WriteLine($"{parsed.Name}: {parsed.Day:yyyy-MM-dd} {parsed.Start:HH:mm}-{parsed.End:HH:mm}");

try { JsonSerializer.Deserialize<Shift>("""{"Name":"Cy","Day":"19.10.2026","Start":"08:00","End":"16:00"}"""); }
catch (JsonException e) { Console.WriteLine(e.Message); }

record Shift(string Name, DateOnly Day, TimeOnly Start, TimeOnly End);
{"Name":"Ana","Day":"2026-10-18","Start":"22:00:00","End":"06:30:15.5000000"}
True
Bo: 2026-10-19 08:00-16:00
The JSON value could not be converted to Shift. Path: $.Day | LineNumber: 0 | BytePositionInLine: 31.

DateOnly is always written as yyyy-MM-dd. TimeOnly is written as HH:mm:ss, with fractional seconds only when they're non-zero. Reading accepts HH:mm too. Other date formats are rejected. If an API sends 19.10.2026, write a custom JsonConverter<DateOnly>. The #:property PublishAot=false line is needed only in file-based apps, which default to Native AOT settings. System.Text.Json covers converters and source generation.

Databases

Database drivers have caught up. Microsoft.Data.Sqlite accepts DateOnly and TimeOnly as parameters and reads them with GetFieldValue<T>:

#:package Microsoft.Data.Sqlite@10.0.13
using Microsoft.Data.Sqlite;

using var conn = new SqliteConnection("Data Source=:memory:");
conn.Open();

using (var cmd = conn.CreateCommand())
{
    cmd.CommandText = "CREATE TABLE holidays (day TEXT NOT NULL, name TEXT, opens TEXT)";
    cmd.ExecuteNonQuery();
}

(DateOnly Day, string Name, TimeOnly? Opens)[] rows =
[
    (new(2026, 11, 11), "Independence Day", new TimeOnly(12, 0)),
    (new(2026, 12, 24), "Christmas Eve", new TimeOnly(10, 0)),
    (new(2026, 12, 25), "Christmas Day", null),
    (new(2027, 1, 1), "New Year's Day", null),
];

foreach (var (day, name, opens) in rows)
{
    using var insert = conn.CreateCommand();
    insert.CommandText = "INSERT INTO holidays VALUES ($day, $name, $opens)";
    insert.Parameters.AddWithValue("$day", day);
    insert.Parameters.AddWithValue("$name", name);
    insert.Parameters.AddWithValue("$opens", (object?)opens ?? DBNull.Value);
    insert.ExecuteNonQuery();
}

using var query = conn.CreateCommand();
query.CommandText = "SELECT day, name, opens FROM holidays WHERE day >= $from ORDER BY day";
query.Parameters.AddWithValue("$from", new DateOnly(2026, 12, 1));
using var reader = query.ExecuteReader();
while (reader.Read())
{
    DateOnly day = reader.GetFieldValue<DateOnly>(0);
    string opens = reader.IsDBNull(2) ? "closed" : $"opens {reader.GetFieldValue<TimeOnly>(2):HH:mm}";
    Console.WriteLine($"{day:ddd yyyy-MM-dd} {reader.GetString(1)}: {opens}");
}

using var raw = conn.CreateCommand();
raw.CommandText = "SELECT day, opens FROM holidays LIMIT 1";
using var r2 = raw.ExecuteReader();
r2.Read();
Console.WriteLine($"stored as: '{r2.GetString(0)}', '{r2.GetString(1)}'");
Thu 2026-12-24 Christmas Eve: opens 10:00
Fri 2026-12-25 Christmas Day: closed
Fri 2027-01-01 New Year's Day: closed
stored as: '2026-11-11', '12:00:00'

SQLite has no date type, so they're stored as ISO 8601 text. That format sorts correctly as text, which is why day >= $from and ORDER BY day work.

Other databases map them to native types:

When writing CSV, use "o" or an explicit invariant format. CSV shows a DateOnly column with CsvHelper.

When not to use them

var start = new DateTime(2026, 10, 18, 22, 0, 0);
var end = new DateTime(2026, 10, 19, 6, 30, 0);
Console.WriteLine(end - start);

// a TimeOnly can't hold "25 hours"; use TimeSpan for durations
TimeSpan worked = TimeSpan.FromHours(25);
Console.WriteLine(worked);
Console.WriteLine(TimeOnly.FromDateTime(start).Add(worked).ToString("HH:mm"));
08:30:00
1.01:00:00
23:00

Adding a 25-hour duration to 22:00 gives 23:00: right as a clock reading, but the extra day is gone unless you use the out wrappedDays overload. For everything about DateTime itself, see the DateTime chapter.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents