C# DateOnly
last modified October 5, 2026
In this article we show how to work with the DateOnly type in C#.
DateOnly represents a date without a time component, with values
ranging from January 1, 0001 through December 31, 9999 in the Gregorian
calendar. Similarly, TimeOnly represents a time of day without a
date component.
DateOnly is a struct located in the System namespace.
It was introduced in .NET 6. Unlike DateTime, a
DateOnly value carries no time or time zone information, which
makes the type a good fit for dates of birth, hire dates, or due dates.
C# DateOnly simple example
The following is a simple example with DateOnly.
using System.Globalization; DateOnly d1 = new DateOnly(2022, 10, 12); Console.WriteLine(d1); DateOnly d2 = new DateOnly(2022, 10, 12, new GregorianCalendar()); Console.WriteLine(d2); DateOnly d3 = new DateOnly(2022, 10, 12, new JulianCalendar()); Console.WriteLine(d3);
The program creates three instances of DateOnly.
DateOnly d1 = new DateOnly(2022, 10, 12);
In the first case, the constructor takes year, month, and day parameters.
DateOnly d2 = new DateOnly(2022, 10, 12, new GregorianCalendar());
In the second case, the constructor also takes the calendar as a parameter.
$ dotnet run 10/12/2022 10/12/2022 10/25/2022
C# DateOnly min & max values
The DateOnly.MaxValue property is the latest possible date that
can be represented, and DateOnly.MinValue is the earliest possible
date.
DateOnly maxVal = DateOnly.MaxValue; DateOnly minVal = DateOnly.MinValue; Console.WriteLine(minVal); Console.WriteLine(maxVal);
The program prints the minimum and maximum values a DateOnly
type can represent.
$ dotnet run 1/1/0001 12/31/9999
C# compare DateOnly
We can compare DateOnly instances with relational operators.
DateOnly d1 = new DateOnly(2022, 10, 12);
DateOnly d2 = new DateOnly(2022, 11, 12);
if (d1 < d2) {
Console.WriteLine($"{d1} is before {d2}");
} else {
Console.WriteLine($"{d1} is after {d2}");
}
The program compares two DateOnly values with the
< operator.
$ dotnet run 10/12/2022 is before 11/12/2022
C# DateOnly.FromDateTime
The DateOnly.FromDateTime method creates a DateOnly
instance from a DateTime object.
DateTime now = DateTime.Now; Console.WriteLine(now); DateOnly d = DateOnly.FromDateTime(now); Console.WriteLine(d); TimeOnly t = TimeOnly.FromDateTime(now); Console.WriteLine(t);
The program gets the current date and time and extracts the date and time parts from it.
DateOnly d = DateOnly.FromDateTime(now); Console.WriteLine(d);
We retrieve the date part with DateOnly.FromDateTime.
TimeOnly t = TimeOnly.FromDateTime(now);
We retrieve the time part with TimeOnly.FromDateTime.
$ dotnet run 10/13/2022 5:31:17 PM 10/13/2022 5:31 PM
C# DateOnly short and long dates
By default, DateOnly is printed using the short date pattern of
the current culture. With the ToLongDateString method, we get a
long date string representation.
DateOnly d = new DateOnly(2022, 10, 12); Console.WriteLine(d); Console.WriteLine(d.ToShortDateString()); Console.WriteLine(d.ToLongDateString());
The program prints a DateOnly in short and long date formats.
$ dotnet run 10/12/2022 10/12/2022 Wednesday, October 12, 2022
C# DateOnly add years, months and days
The AddYears, AddMonths, and AddDays
methods add years, months, and days to DateOnly instances. They
return a new DateOnly value; the original instance remains
unchanged.
DateOnly d = new DateOnly(2022, 10, 10); var res1 = d.AddYears(2).AddMonths(11).AddDays(3); Console.WriteLine(res1); var res2 = d.AddYears(-2).AddMonths(-11).AddDays(-3); Console.WriteLine(res2);
In the program, we apply these three methods to the same date.
$ dotnet run 9/13/2025 11/7/2019
C# DateOnly TryParse
The TryParse method converts a string to a
DateOnly value.
using System.Globalization;
if (DateOnly.TryParse("10/9/2022", new CultureInfo("en-US"),
DateTimeStyles.None, out var res))
{
Console.WriteLine(res.ToLongDateString());
}
if (DateOnly.TryParse("10/9/2022", new CultureInfo("sk-SK"),
DateTimeStyles.None, out var res2))
{
Console.WriteLine(res2.ToLongDateString());
}
The 10/9/2022 date has a different meaning in the US and Slovak
cultures, because parsing is culture-sensitive.
$ dotnet run Sunday, October 9, 2022 Saturday, September 10, 2022
Source
In this article we have shown how to work with the DateOnly type in C#.
Author
List all C# tutorials.