Learn C# in Y Minutes
C# 14 · .NET 10

11. Dates & Times

Orders have a creation time, subscriptions expire, tokens time out. .NET has a small family of types for this: DateTime, TimeSpan, DateOnly, TimeOnly, and DateTimeOffset.

DateTime

A DateTime is a date plus a time of day. Create one with the constructor, then read parts of it through properties like Year and DayOfWeek.

DateTime is immutable: AddDays, AddMonths, and friends never change the original. They return a new value, so always use the result. Pass a negative number to go back in time.

app/11-dates/CreatingDates.cs
using System.Globalization;

CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;

var launch = new DateTime(2026, 9, 28, 14, 30, 0);

Console.WriteLine(launch);
Console.WriteLine($"Year: {launch.Year}, Month: {launch.Month}, Day: {launch.Day}");
Console.WriteLine($"Day of week: {launch.DayOfWeek}");
Console.WriteLine($"Day of year: {launch.DayOfYear}");

var tomorrow = launch.AddDays(1);
var nextMonth = launch.AddMonths(1);
var earlier = launch.AddHours(-3);

Console.WriteLine($"Tomorrow: {tomorrow}");
Console.WriteLine($"Next month: {nextMonth}");
Console.WriteLine($"3 hours earlier: {earlier}");
Console.WriteLine($"Tomorrow is later? {tomorrow > launch}");
09/28/2026 14:30:00
Year: 2026, Month: 9, Day: 28
Day of week: Monday
Day of year: 271
Tomorrow: 09/29/2026 14:30:00
Next month: 10/28/2026 14:30:00
3 hours earlier: 09/28/2026 11:30:00
Tomorrow is later? True

How a date prints depends on the machine's culture: US machines show 09/28/2026, Indonesian ones 28/09/2026. The line CultureInfo.CurrentCulture = CultureInfo.InvariantCulture makes these examples print the same everywhere.

Formatting and parsing

Pass a format string to ToString, or put it after a colon inside interpolation. The common pieces: yyyy year, MM month, dd day, HH 24-hour, hh 12-hour, mm minutes, ss seconds. Capital MM is month, small mm is minutes.

To go the other way, Parse reads a string into a DateTime. ParseExact is stricter: the text must match the format you give.

app/11-dates/Formatting.cs
using System.Globalization;

CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;

var date = new DateTime(2026, 9, 28, 14, 5, 9);

Console.WriteLine(date.ToString("yyyy-MM-dd"));
Console.WriteLine(date.ToString("dd/MM/yyyy HH:mm"));
Console.WriteLine(date.ToString("dddd, MMMM d, yyyy"));
Console.WriteLine(date.ToString("h:mm tt"));
Console.WriteLine($"{date:MMM d}");

var parsed = DateTime.Parse("2026-12-25");
Console.WriteLine($"Parsed: {parsed:yyyy-MM-dd} is a {parsed.DayOfWeek}");

var exact = DateTime.ParseExact("25/12/2026", "dd/MM/yyyy", CultureInfo.InvariantCulture);
Console.WriteLine($"ParseExact: {exact:yyyy-MM-dd}");
2026-09-28
28/09/2026 14:05
Monday, September 28, 2026
2:05 PM
Sep 28
Parsed: 2026-12-25 is a Friday
ParseExact: 2026-12-25

TimeSpan

A TimeSpan is a length of time, not a point in time: "2 hours 15 minutes". Subtracting two dates gives you a TimeSpan.

Watch the difference: Minutes is only the minutes part (15), while TotalMinutes is the whole span in minutes (135).

app/11-dates/TimeSpans.cs
var movie = new TimeSpan(2, 15, 0);
var ad = TimeSpan.FromMinutes(90);

Console.WriteLine($"Movie: {movie}");
Console.WriteLine($"Hours: {movie.Hours}, Minutes: {movie.Minutes}");
Console.WriteLine($"TotalMinutes: {movie.TotalMinutes}");
Console.WriteLine($"Ad: {ad}");
Console.WriteLine($"Together: {movie + ad}");

var today = new DateTime(2026, 9, 28);
var christmas = new DateTime(2026, 12, 25);
TimeSpan left = christmas - today;

Console.WriteLine($"Days until Christmas: {left.Days}");
Console.WriteLine($"Christmas minus 1 week: {(christmas - TimeSpan.FromDays(7)):yyyy-MM-dd}");
Movie: 02:15:00
Hours: 2, Minutes: 15
TotalMinutes: 135
Ad: 01:30:00
Together: 03:45:00
Days until Christmas: 88
Christmas minus 1 week: 2026-12-18

DateOnly and TimeOnly

A birthday has no time. Opening hours have no date. DateOnly and TimeOnly say exactly that, so nobody wonders what the 00:00 in a birthday means.

app/11-dates/DateOnlyTimeOnly.cs
using System.Globalization;

CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;

var birthday = new DateOnly(1995, 4, 17);
var opens = new TimeOnly(9, 0);
var closes = new TimeOnly(17, 30);

Console.WriteLine($"Birthday: {birthday:yyyy-MM-dd}");
Console.WriteLine($"Opens: {opens:HH:mm}, closes: {closes:HH:mm}");

var now = new TimeOnly(12, 45);
Console.WriteLine($"Open at 12:45? {now.IsBetween(opens, closes)}");

var meeting = new DateTime(2026, 9, 28, 10, 30, 0);
Console.WriteLine($"Date part: {DateOnly.FromDateTime(meeting):yyyy-MM-dd}");
Console.WriteLine($"Time part: {TimeOnly.FromDateTime(meeting):HH:mm}");
Birthday: 1995-04-17
Opens: 09:00, closes: 17:30
Open at 12:45? True
Date part: 2026-09-28
Time part: 10:30

UTC, offsets, and ISO 8601

"14:00" means nothing without a time zone. UTC is the global reference time; servers should store and compare times in UTC. Get the current one with DateTime.UtcNow, not DateTime.Now (which depends on where the server runs).

A DateTimeOffset stores the time and its offset from UTC, so it always points to one exact instant. Jakarta 14:00 (+07:00) and London 08:00 (+01:00) are equal. The "o" format prints ISO 8601, the standard way APIs exchange dates.

app/11-dates/UtcAndOffset.cs
var utc = new DateTime(2026, 9, 28, 7, 0, 0, DateTimeKind.Utc);
Console.WriteLine($"Kind: {utc.Kind}");
Console.WriteLine($"ISO 8601: {utc:o}");

var jakarta = new DateTimeOffset(2026, 9, 28, 14, 0, 0, TimeSpan.FromHours(7));
Console.WriteLine($"Jakarta: {jakarta:o}");
Console.WriteLine($"Same moment in UTC: {jakarta.UtcDateTime:o}");

var london = new DateTimeOffset(2026, 9, 28, 8, 0, 0, TimeSpan.FromHours(1));
Console.WriteLine($"Same instant as London 08:00? {jakarta == london}");

var fromApi = DateTimeOffset.Parse("2026-09-28T07:00:00Z");
Console.WriteLine($"Parsed offset: {fromApi.Offset}");
Kind: Utc
ISO 8601: 2026-09-28T07:00:00.0000000Z
Jakarta: 2026-09-28T14:00:00.0000000+07:00
Same moment in UTC: 2026-09-28T07:00:00.0000000Z
Same instant as London 08:00? True
Parsed offset: 00:00:00

ASP.NET Core serializes DateTime and DateTimeOffset to ISO 8601 JSON automatically, and parses them back from request bodies. Store DateTimeOffset.UtcNow in your records and let clients convert to local time.

Program.cs
app.MapGet("/time", () => new { now = DateTimeOffset.UtcNow });
GET /time
{ "now": "2026-09-28T07:00:00.1234567+00:00" }

Quick quiz