Skip to main content

How to Format a Date and Time with a Custom Format String in C#

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

moment.ToString("yyyy-MM-dd HH:mm:ss zzz", invariant) writes a date and time exactly as 2026-11-26 14:05:09 +01:00. Every letter in the format stands for one part of the date or the time.

Microsoft's guide: A date and time format string defines the text representation of a DateTime or DateTimeOffset value that results from a formatting operation. A custom one lists the parts one by one, such as yyyy for the year.

Syntax​

Syntax
string <text> = <dateTime>.ToString("<custom format>", <culture>);
SpecifierWhat it writes
yyyyThe year as a four-digit number.
MMThe month, from 01 to 12.
ddThe day of the month, from 01 to 31.
HHThe hour, using a 24-hour clock from 00 to 23.
mmThe minute, from 00 to 59.
ssThe second, from 00 to 59.
ddddThe full name of the day of the week.
zzzHours and minutes offset from UTC.
'text'The text in single quotes, as it is

The Example​

A console app called DateFormats writes one moment, 26 November 2026 at 14:05:09, one hour ahead of UTC, in four formats: one for computers, an American one, a German one and a German time with a word.

Prerequisites​

  • The .NET 10 SDK. dotnet --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n DateFormats
cd DateFormats
Output
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/you/source/DateFormats/DateFormats.csproj:
Determining projects to restore...
Restored /Users/you/source/DateFormats/DateFormats.csproj (in 46 ms).
Restore succeeded.

dotnet new console -n DateFormats creates a folder named DateFormats with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or DateFormats.csproj in Visual Studio.

Step 2: Write the Code​

Replace everything in Program.cs. First the moment and the cultures:

Program.cs (the moment)
using System.Globalization;

// 26 November 2026, 14:05:09, one hour ahead of UTC.
DateTimeOffset moment = new(2026, 11, 26, 14, 5, 9, TimeSpan.FromHours(1));

CultureInfo invariant = CultureInfo.InvariantCulture;
CultureInfo us = CultureInfo.GetCultureInfo("en-US");
CultureInfo german = CultureInfo.GetCultureInfo("de-DE");

The reference for CultureInfo.InvariantCulture: The invariant culture is culture-insensitive; it's associated with the English language but not with any country/region.

Then the four formats:

Program.cs (the formats)
Console.WriteLine(moment.ToString("yyyy-MM-dd HH:mm:ss zzz", invariant));
Console.WriteLine(moment.ToString("dddd, d MMMM yyyy", us));
Console.WriteLine(moment.ToString("dddd, d. MMMM yyyy", german));
Console.WriteLine(moment.ToString("HH:mm 'Uhr'", german));

'Uhr' in single quotes is written as it is: it's German for o'clock.

Step 3: Run It​

Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:

Terminal
dotnet run
Output
2026-11-26 14:05:09 +01:00
Thursday, 26 November 2026
Donnerstag, 26. November 2026
14:05 Uhr

macOS-style terminal window titled Terminal — DateFormats showing dotnet run printing 2026-11-26 14:05:09 +01:00, Thursday, 26 November 2026, Donnerstag, 26. November 2026 and 14:05 Uhr

The same moment came out four ways. dddd and MMMM gave English names with en-US and German ones with de-DE.

The Complete File​

macOS-style code window titled Program.cs — DateFormats showing a DateTimeOffset for 26 November 2026 at 14:05:09, the invariant, en-US and de-DE cultures, and four ToString calls with custom date and time formats

Program.cs
using System.Globalization;

// 26 November 2026, 14:05:09, one hour ahead of UTC.
DateTimeOffset moment = new(2026, 11, 26, 14, 5, 9, TimeSpan.FromHours(1));

CultureInfo invariant = CultureInfo.InvariantCulture;
CultureInfo us = CultureInfo.GetCultureInfo("en-US");
CultureInfo german = CultureInfo.GetCultureInfo("de-DE");

Console.WriteLine(moment.ToString("yyyy-MM-dd HH:mm:ss zzz", invariant));
Console.WriteLine(moment.ToString("dddd, d MMMM yyyy", us));
Console.WriteLine(moment.ToString("dddd, d. MMMM yyyy", german));
Console.WriteLine(moment.ToString("HH:mm 'Uhr'", german));

Common Mistakes​

MistakeWhat happensDo this instead
mm where you meant MMmm is minutes: yyyy-mm-dd wrote 2026-05-26, with the 5 minutes in place of the monthUse MM for the month and mm for minutes
hh for a 24-hour timehh is the 12-hour clock: hh:mm wrote 02:05 for 14:05Use HH, or add tt for AM or PM: hh:mm tt wrote 02:05 PM
Letters in the format that are specifiersdd Month wrote 26 11onP2: M became the month, t the first letter of PM and h the hourPut the text in single quotes, as 'Uhr' is, or put a backslash before a letter: HH\h mm wrote 14h 05

FAQ​

Where do the day and month names come from?​

From the culture. The guide: The localized name of the day of the week is retrieved from the DateTimeFormatInfo.DayNames property of the current or specified culture. With de-DE, Thursday became Donnerstag.

Conclusion​

Spell out a date and time with a custom format string, letter by letter, and pass a culture for the names. Keep MM for months and mm for minutes, use HH for a 24-hour clock, and put text in quotes.

Additional Resources​