DateOnly & TimeOnly in C#
Calendar dates and wall-clock times, without the fake midnight.
var today = new DateOnly(2026, 8, 1);
var release = new DateOnly(2026, 11, 10);
Console.WriteLine(release.DayOfWeek);
Console.WriteLine(today.AddMonths(2));
// DayNumber turns calendar distance into plain integers
var wait = release.DayNumber - today.DayNumber;
Console.WriteLine($"{wait} days to go");
var standup = new TimeOnly(9, 30);
Console.WriteLine(standup.AddHours(2.5));
How it works
DateOnlycarries no time, so date math stays honest.DayNumbersubtraction turns a wait into plain integers.TimeOnly.AddHours(2.5)handles the fractional hour.
Keywords and builtins used here
newvar
The run, in numbers
- Lines
- 12
- Characters to type
- 382
- Tokens
- 89
- Three-star pace
- 95 tpm
At the three-star pace of 95 tokens a minute, this run takes about 56 seconds.
Step 1 of 3 in Dates & times, step 7 of 17 in The .NET library.