C# DateOnly and TimeOnly: Model a Date Without a Hidden Clock
A birthday is a calendar date, not midnight in a particular time zone. A shop’s opening time is a time of day, not an elapsed duration. Yet C# programs often squeeze both into DateTime, leaving a time or date component that can confuse comparisons, serialization and database storage. .NET’s DateOnly and TimeOnly types make that intent explicit.
Editorial illustration; the types belong to .NET, and the examples use C#.
Summary
DateOnlyrepresents a calendar date;TimeOnlyrepresents a time of day.- Both types were introduced in .NET 6 and are unavailable in .NET Framework.
- Use
DateTimeOffsetor time-zone-aware logic when a value represents a real instant or appointment across zones.
Choose the value that matches the meaning
Microsoft describes DateOnly as a date without a time component and TimeOnly as a time within a 24-hour day. DateOnly suits a birthday, invoice date or holiday. TimeOnly suits a recurring opening time such as 09:30. TimeSpan represents an elapsed duration, so it is usually the better fit for “wait 45 minutes,” not “the doors open at 09:30.”
This is a .NET API distinction, not a new C# language feature. The types are available in .NET 6 and later, but not .NET Framework. Older targets need another representation or a compatible library, and serialization/database behavior should be checked against the particular stack.
A small C# example
The following console example targets .NET 6 or later. Top-level statements and using directives are supported by the project’s C# version; the types themselves come from the .NET target framework.
using System.Globalization;
DateOnly invoiceDate = new(2026, 11, 25);
TimeOnly opensAt = new(9, 30);
Console.WriteLine(invoiceDate.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
Console.WriteLine(opensAt.ToString("HH:mm", CultureInfo.InvariantCulture));
DateOnly deliveryDate = invoiceDate.AddDays(3);
TimeOnly reminder = opensAt.AddMinutes(-15);
Console.WriteLine($"Delivery: {deliveryDate:yyyy-MM-dd}");
Console.WriteLine($"Reminder: {reminder:HH:mm}");
Both are immutable value types: methods such as AddDays and AddMinutes return a new value. They do not mutate the original variable unless you assign the result. TimeOnly.Add... wraps around the 24-hour day; if crossing midnight is important, use an overload that reports the day difference or combine the time with a date deliberately.
Parse input with a known format
For user or file input, avoid culture-dependent assumptions when the format is fixed. A date such as 03/04/2026 is ambiguous across locales. ParseExact makes the expected representation clear:
using System.Globalization;
string rawDate = "2026-11-25";
string rawTime = "09:30";
if (!DateOnly.TryParseExact(rawDate, "yyyy-MM-dd",
CultureInfo.InvariantCulture, DateTimeStyles.None, out var date))
{
throw new FormatException("Expected date in yyyy-MM-dd format.");
}
if (!TimeOnly.TryParseExact(rawTime, "HH:mm",
CultureInfo.InvariantCulture, DateTimeStyles.None, out var time))
{
throw new FormatException("Expected time in 24-hour HH:mm format.");
}
In an application, return a validation message instead of throwing if invalid input is an ordinary user mistake. For localized forms, choose a culture intentionally and display a matching example. The stored or exchanged format should be agreed with the API or database rather than guessed from the current machine.
Serialization and persistence
System.Text.Json supports serializing and deserializing DateOnly and TimeOnly starting with .NET 7. If you target .NET 6, verify your serializer and package version or provide an explicit converter. A type available at compile time does not guarantee that every framework serializer, ORM provider or API client handles it automatically.
For SQL Server, a date column maps naturally to a date-only value; inspect the provider version and migration output. Keep the domain meaning intact: do not turn a date-only value into midnight UTC just to make a timestamp-shaped field accept it. That can shift the calendar day when displayed in another time zone.
When these types are not enough
A meeting invitation for a specific instant needs more than TimeOnly. “2026-11-25 at 09:30 in Kuala Lumpur” has a date, local time and time zone. A fixed instant is commonly represented with DateTimeOffset; recurring local schedules need a time-zone identifier and rules for daylight-saving transitions where relevant.
A DateOnly value cannot identify a moment on the global timeline, and TimeOnly cannot say which day a shift belongs to. Use them only when the missing component is genuinely irrelevant to the business meaning.
Practical decision check
- Calendar fact with no time:
DateOnly. - Recurring wall-clock time:
TimeOnly. - Duration or elapsed wait:
TimeSpan. - Timestamp with offset or known instant:
DateTimeOffset. - Future local appointment in a named region: date + local time + time-zone identifier, with explicit conversion rules.
This check prevents a common bug: storing something simple as DateTime, then inventing policies for whether its midnight is local, UTC or unspecified. It also makes method signatures clearer to the next developer.
Common mistakes
- Using
TimeOnlyfor an appointment without storing its date or zone. - Converting a date-only value to midnight UTC and accidentally changing its displayed day.
- Assuming .NET Framework includes these types.
- Assuming .NET 6
System.Text.Jsonhandles them exactly like .NET 7+. - Forgetting that adding time can wrap to the next or previous day.
If you are improving async API code, see our C# cancellation token guide. For a different .NET 10 capability, read the C# 14 extension members overview.
My Take
Use the narrowest type that says what the value means, then preserve that meaning across the database and API boundary. DateOnly is a small change that removes a surprising number of midnight and time-zone assumptions from ordinary business code.
Bottom Line
Use DateOnly for a date and TimeOnly for a time of day on .NET 6+, but use a timestamp and explicit time-zone logic for real appointments. Check framework and serializer support before changing a public contract.