Format dates and offsets predictably in C#

Date formatting and date parsing solve different problems. Construct or parse the value first, then choose the representation its reader needs.

Use DateTimeOffset when an offset is part of the value. A formatted offset does not identify a time zone or its daylight-saving rules.

Format a known value

Create a .NET console project and use this Program.cs:

C#
using System.Globalization;

var timestamp = new DateTimeOffset(2026, 11, 24, 16, 5, 7, TimeSpan.FromHours(10));
Console.WriteLine(timestamp.ToString("yyyy-MM-dd HH:mm:ss zzz",
	CultureInfo.InvariantCulture));
Console.WriteLine(timestamp.ToString("O", CultureInfo.InvariantCulture));
Console.WriteLine(timestamp.ToString("d MMMM yyyy", CultureInfo.GetCultureInfo("en-AU")));

Run dotnet run. The first line is 2026-11-24 16:05:07 +10:00. The second is 2026-11-24T16:05:07.0000000+10:00. The third is 24 November 2026.

Pick tokens deliberately

MM means month. mm means minute. HH is a 24-hour clock, while hh needs an AM/PM marker to avoid ambiguity. yyyy keeps the four-digit year. The round-trip O format is useful when a string must preserve the offset and precision.

Avoid parsing inputs such as 08/09/26 without a known format and culture. Use TryParseExact for a fixed input contract. For storage and APIs, prefer typed date/time fields or a documented round-trip representation. Reserve localized month names and punctuation for display.

Official references

Custom date and time format strings - .NET
Learn to use custom date and time format strings to convert DateTime or DateTimeOffset values into text representations, or to parse strings for dates & times.
https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings
Custom date and time formats

Author: Herman Schoenfeld