Dates, times and durations
Choose a date/time type according to what the value means. A calendar date, a clock time, a timestamp identifying an instant, and a duration are different concepts. Using separate types helps avoid accidental time-zone conversions.
using System;using System.Globalization;var due = new DateOnly(2026, 2, 28);Console.WriteLine(due.AddDays(1).ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));var opening = new TimeOnly(9, 30);Console.WriteLine(opening.ToString("HH:mm", CultureInfo.InvariantCulture));Date arithmetic crosses the month boundary; a clock time needs no date.
Expected output (reasoned, not executed):
2026-03-0109:30Represent calendar and clock values
DateOnly represents a date without a time or offset. TimeOnly represents a clock time without a date. They fit a due date and an opening time respectively. Both were introduced in .NET 6. A TimeSpan represents a duration, which may exceed 24 hours or be negative.
Represent an instant
DateTimeOffset combines a date and time with a UTC offset, identifying a particular instant. An offset alone does not identify a named time zone. DateTime has a Kind (Utc, Local or Unspecified); an unspecified value does not identify an instant without additional context. Use explicit fixed timestamps when testing.
Calculate elapsed time, not wall-clock differences
Subtract two DateTimeOffset values to get a TimeSpan. TotalMinutes includes the hours and days; Minutes is just the minute component. For schedules governed by regional daylight-saving rules use TimeZoneInfo and handle invalid and ambiguous local times explicitly. The current local offset is not a permanent rule. In the first examples we use invariant formatting to keep output predictable.
using System;using System.Globalization;var start = new DateTimeOffset(2026, 6, 1, 10, 0, 0, TimeSpan.FromHours(2));var end = new DateTimeOffset(2026, 6, 1, 9, 30, 0, TimeSpan.Zero);TimeSpan elapsed = end - start;Console.WriteLine(elapsed.TotalMinutes.ToString(CultureInfo.InvariantCulture));Console.WriteLine(elapsed.Minutes);The starting instant is 08:00 UTC and the ending instant is 09:30 UTC.
Expected output (reasoned, not executed):
9030using System;using System.Globalization;var local = new DateTimeOffset(2026, 1, 5, 14, 0, 0, TimeSpan.FromHours(3));var utc = local.ToUniversalTime();Console.WriteLine(utc.ToString("HH:mm zzz", CultureInfo.InvariantCulture));Console.WriteLine(local == utc);The clock display changes, but equality compares the represented instant.
Expected output (reasoned, not executed):
11:00 +00:00TrueCommon mistake and correction
Do not use DateTime.Now for a deterministic lesson test or assume DateTimeOffset remembers a region. A future appointment at a named local time requires its time-zone rules and a policy for ambiguous or nonexistent times. For durations use TimeSpan rather than a clock time.
Try it yourself
An event starts at 08:15 UTC and ends at 10:00 UTC on 2026-06-01. Construct both as DateTimeOffset values, subtract, and report TotalMinutes and Minutes.
Next: Culture-aware formatting. Examples target C# 14 / net10.0. They are static study material; compiler and ByteMind execution have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Dates, times and durations — knowledge check
Apply the lesson concepts to the following scenarios.
Which type represents a birthday without a time?
Which type represents 30 hours of elapsed time?
Does an offset of +02:00 identify a unique time zone?
What is TotalMinutes for 1 hour 45 minutes?
What does ToUniversalTime on DateTimeOffset preserve?