Read the syntax
Use DateOnly and TimeOnly for calendar dates and daily clock times without inventing an instant.
AddDays returns the new date, including the month boundary.
What it is and why to use it
A birthday is a date, and a shop opening time is a time of day. Neither necessarily identifies a global instant. DateOnly and TimeOnly, introduced in .NET 6, represent those concepts directly. They avoid attaching an arbitrary date or time component simply to use DateTime. They are not available as built-in types in the older .NET Framework.
How it works
DateOnly stores a calendar date and supports operations such as AddDays. TimeOnly stores a clock value within a day and can wrap when adding hours. Combining them can create a DateTime, but choosing a real-world instant still needs a time zone and a policy for ambiguous or invalid local times. Formatting uses an explicit culture when deterministic output is required.
Common mistakes and limitations
Do not treat DateOnly as a time zone or TimeOnly as a duration; use TimeSpan for an elapsed duration. A local meeting time still needs a zone. Adding hours to a TimeOnly can cross midnight without preserving a calendar date unless you track the day change.
Run the examples locally
These complete Program.cs examples use a .NET 10 console project with the C# language version set to 12.0, nullable checking enabled and implicit usings disabled. They use standard .NET libraries and need no extra NuGet packages. This is the verified environment, not a claim that every API requires .NET 10 or works unchanged in .NET Framework, Unity or other runtimes.
Install a supported .NET 10 SDK, create the console project, replace its project file with the settings below, then replace Program.cs with one complete example at a time. Build and run from the project folder. using directives bring namespaces into scope; top-level statements form the program’s entry point, and any helper types follow those statements. Console.WriteLine writes terminal output. Do not combine examples that declare the same type names.
C# in Codectionary’s Playground is editing-only: it does not compile or execute these programs. Run them with the local .NET SDK to reproduce the expected output. Async examples use controlled in-process operations rather than live network services or timing-sensitive delays.
Create and enter a project
dotnet new console -n CSharpExample --framework net10.0
cd CSharpExampleCSharpExample.csproj settings used for these examples
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<LangVersion>12.0</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>
</PropertyGroup>
</Project>Build and run after saving one example in Program.cs
dotnet build
dotnet run --no-buildContinue learning
Change one input and predict the result before running it. Follow the related guides to connect this topic with the surrounding language features.
Syntax
var date = new DateOnly(2026, 10, 29);
var time = new TimeOnly(9, 30);Examples
Move a calendar deadline
Add days to a date without an unused time-of-day component.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Globalization;
var start = new DateOnly(2026, 10, 29);
var due = start.AddDays(3);
Console.WriteLine(start.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
Console.WriteLine(due.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
- DateOnly expresses a calendar date rather than a timestamp.
- AddDays returns the new date, including the month boundary.
- The original is unchanged and an explicit format prints unambiguous year-month-day text.
- Expected output: 2026-10-29 2026-11-01
- Choose types that match dates, clock times, durations or instants.
Track a clock time crossing midnight
Use the overload that reports how many days were wrapped.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Globalization;
var closing = new TimeOnly(23, 30);
var later = closing.AddHours(2, out int wrappedDays);
Console.WriteLine(later.ToString("HH:mm", CultureInfo.InvariantCulture));
Console.WriteLine($"Day change: {wrappedDays}");
- Adding two hours crosses the end of the day.
- TimeOnly wraps to 01:30 and reports one wrapped day through the out parameter.
- An actual schedule can use that day change with its date; the clock value alone is not a complete appointment.
- Expected output: 01:30 Day change: 1
- Choose types that match dates, clock times, durations or instants.
Best practices
- Choose types that match dates, clock times, durations or instants.
- Keep time-zone decisions explicit when combining local date/time values.
Runtime & versions
.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
C# language reference