Data and Formats · Lesson 7

Dates without time zones

Use DateOnly when the domain means a calendar date.

How it works

A calendar date such as a due date does not always represent an instant on a global timeline. DateOnly expresses a year, month, and day without silently adding a time zone. TryParseExact accepts the documented yyyy-MM-dd contract here, and AddDays performs calendar arithmetic across a month boundary. Use DateTimeOffset when a timestamp must identify an instant with an offset. Converting a date into midnight in an arbitrary time zone changes the domain meaning and can cause avoidable boundary bugs.

Keep storage, display, and comparison rules aligned with whether the value is a date, a local appointment, or an instant.

Try an impossible date such as February thirtieth and check the boolean result. Then explain why a birthday and an event’s exact start time need different representations.

A working example

using System;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;

if (DateOnly.TryParseExact("2026-01-31", "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out var date))
    Console.WriteLine(date.AddDays(1).ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));

Expected output

2026-02-01
Try this
A date-only value should not acquire an accidental time zone.