Skip to main content

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

· 9 min read
Jagdish Kumawat
Founder @ Dewiride

moment.ToString("O") writes a date and time as 2026-03-07T14:05:09.0000000+00:00, an ISO 8601 text that other programs read back without guessing. One letter, such as O, s, u, R, d or D, stands for a whole pattern.

Microsoft's guide: A standard date and time format string uses a single character as the format specifier to define the text representation of a DateTime, DateTimeOffset, DateOnly, or TimeOnly value. And: Any date and time format string that contains more than one character, including white space, is interpreted as a custom date and time format string.

Syntax​

Syntax
string <text> = <value>.ToString("<specifier>", <culture>);
string <text> = $"... {<value>:<specifier>} ...";
PartWhat it is
<value>A DateTime, DateTimeOffset, DateOnly or TimeOnly
<specifier>One letter from the table below. Upper and lower case can mean different things
<culture>A CultureInfo. It decides the order, the separators and the names for the letters that follow the culture
SpecifierWhat it writes, in the guide's words
O or oround-trip date/time pattern
sSortable date/time pattern.
uUniversal sortable date/time pattern.
R or rRFC1123 pattern.
dShort date pattern.
DLong date pattern.
tShort time pattern.
gGeneral date/time pattern (short time).

About O, R, s and u, the guide says: These strings correspond to custom format strings defined by the invariant culture. They produce string representations of date and time values that are intended to be identical across cultures. The other letters follow the culture.

The Example​

A console app called StandardDateFormats formats one moment, 7 March 2026 at 14:05:09 UTC, with the four letters that don't depend on the culture, then with O inside an interpolated string, then with d and D for the United States and for Germany.

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 StandardDateFormats
cd StandardDateFormats
Output
The template "Console App" was created successfully.

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

dotnet new console -n StandardDateFormats creates a folder named StandardDateFormats 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 StandardDateFormats.csproj in Visual Studio.

Step 2: Write the Code​

Replace everything in Program.cs. First the moment and the formats for computers:

Program.cs (formats for computers)
using System.Globalization;

// One moment: 7 March 2026 at 14:05:09 UTC (an offset of zero from UTC).
DateTimeOffset moment = new(2026, 3, 7, 14, 5, 9, TimeSpan.Zero);
CultureInfo invariant = CultureInfo.InvariantCulture;

// For computers: these four come out the same in every culture.
Console.WriteLine($"O: {moment.ToString("O", invariant)}");
Console.WriteLine($"s: {moment.ToString("s", invariant)}");
Console.WriteLine($"u: {moment.ToString("u", invariant)}");
Console.WriteLine($"R: {moment.ToString("R", invariant)}");
  • new(2026, 3, 7, 14, 5, 9, TimeSpan.Zero) is 7 March 2026 at 14:05:09 with an offset of zero from UTC. A fixed moment gives the same output on every run.
  • ToString("O", invariant) formats it with the O pattern. invariant is CultureInfo.InvariantCulture, from System.Globalization. For O the guide says that the formatting or parsing operation that uses the specifier always uses the invariant culture, and it says the same of R, s and u.

Then a format inside an interpolated string, and two formats for people:

Program.cs (interpolation and formats for people)
// In an interpolated string, the format goes after a colon.
Console.WriteLine($"The sale ends at {moment:O}.");

// For people: these follow the culture you pass.
CultureInfo us = CultureInfo.GetCultureInfo("en-US");
CultureInfo german = CultureInfo.GetCultureInfo("de-DE");
Console.WriteLine($"d (en-US): {moment.ToString("d", us)}");
Console.WriteLine($"d (de-DE): {moment.ToString("d", german)}");
Console.WriteLine($"D (en-US): {moment.ToString("D", us)}");
Console.WriteLine($"D (de-DE): {moment.ToString("D", german)}");
  • {moment:O} puts the format after a colon inside the braces of an interpolated string.
  • CultureInfo.GetCultureInfo("en-US") gets a culture by its name: English in the United States. de-DE is German in Germany.

Step 3: Run It​

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

Terminal
dotnet run
Output
O: 2026-03-07T14:05:09.0000000+00:00
s: 2026-03-07T14:05:09
u: 2026-03-07 14:05:09Z
R: Sat, 07 Mar 2026 14:05:09 GMT
The sale ends at 2026-03-07T14:05:09.0000000+00:00.
d (en-US): 3/7/2026
d (de-DE): 07.03.2026
D (en-US): Saturday, March 7, 2026
D (de-DE): Samstag, 7. März 2026

macOS-style terminal window titled Terminal — StandardDateFormats showing dotnet run printing the moment as 2026-03-07T14:05:09.0000000+00:00 with O, 2026-03-07T14:05:09 with s, 2026-03-07 14:05:09Z with u, Sat, 07 Mar 2026 14:05:09 GMT with R, the O text inside a sentence, and d and D for en-US and de-DE

  • O wrote every part, down to seven digits of fractions of a second, and the offset +00:00. Inside the interpolated string it gave the same text.
  • s and u wrote year, month, day, hours, minutes and seconds, in that order. u ended with Z for UTC.
  • R wrote the RFC 1123 form, with English day and month names and GMT.
  • d gave 3/7/2026 for the United States and 07.03.2026 for Germany, and D wrote the day and month names in each language.

The Complete File​

macOS-style code window titled Program.cs — StandardDateFormats showing a DateTimeOffset for 7 March 2026 at 14:05:09 with a zero offset, ToString calls with O, s, u and R and the invariant culture, an interpolated string with moment, and d and D for the en-US and de-DE cultures

Program.cs
using System.Globalization;

// One moment: 7 March 2026 at 14:05:09 UTC (an offset of zero from UTC).
DateTimeOffset moment = new(2026, 3, 7, 14, 5, 9, TimeSpan.Zero);
CultureInfo invariant = CultureInfo.InvariantCulture;

// For computers: these four come out the same in every culture.
Console.WriteLine($"O: {moment.ToString("O", invariant)}");
Console.WriteLine($"s: {moment.ToString("s", invariant)}");
Console.WriteLine($"u: {moment.ToString("u", invariant)}");
Console.WriteLine($"R: {moment.ToString("R", invariant)}");

// In an interpolated string, the format goes after a colon.
Console.WriteLine($"The sale ends at {moment:O}.");

// For people: these follow the culture you pass.
CultureInfo us = CultureInfo.GetCultureInfo("en-US");
CultureInfo german = CultureInfo.GetCultureInfo("de-DE");
Console.WriteLine($"d (en-US): {moment.ToString("d", us)}");
Console.WriteLine($"d (de-DE): {moment.ToString("d", german)}");
Console.WriteLine($"D (en-US): {moment.ToString("D", us)}");
Console.WriteLine($"D (de-DE): {moment.ToString("D", german)}");

Common Mistakes​

MistakeWhat happensDo this instead
Using d or D for text that a program reads backThe text changes with the culture: d in en-US wrote 7 March as 3/7/2026, and en-GB read that back as Friday, 03 July 2026Use O for text that is stored or sent: read back with en-GB, it gave the same moment, True
U with a DateTimeOffsetIt throws FormatException: Input string was not in a correct format. The guide: The "U" format specifier is not supported by the DateTimeOffset type and throws a FormatException if it is used to format a DateTimeOffset value.Format its UtcDateTime, a DateTime: moment.UtcDateTime.ToString("U", CultureInfo.InvariantCulture) gave Saturday, 07 March 2026 14:05:09
s for a moment that isn't at UTCs drops the offset: 14:05:09 at +00:00 and 14:05:09 at +01:00, an hour apart, both gave 2026-03-07T14:05:09Use O, which kept +00:00 and +01:00, or u, which turned the second one into 13:05:09Z

FAQ​

Why does O end with Z for some values and +00:00 for others?​

O writes what the value knows about its time zone. The guide: The time zone component of DateTimeKind.Utc date and time values uses "Z" (which stands for zero offset) to represent UTC. And: DateTimeKind.Unspecified date and time values have no time zone information. The same moment three ways:

Program.cs
using System.Globalization;

CultureInfo invariant = CultureInfo.InvariantCulture;
DateTime utc = new(2026, 3, 7, 14, 5, 9, DateTimeKind.Utc);
DateTime unspecified = new(2026, 3, 7, 14, 5, 9);
DateTimeOffset offset = new(2026, 3, 7, 14, 5, 9, TimeSpan.Zero);

Console.WriteLine($"DateTime, Kind Utc: {utc.ToString("O", invariant)}");
Console.WriteLine($"DateTime, Kind Unspecified: {unspecified.ToString("O", invariant)}");
Console.WriteLine($"DateTimeOffset: {offset.ToString("O", invariant)}");
Output
DateTime, Kind Utc: 2026-03-07T14:05:09.0000000Z
DateTime, Kind Unspecified: 2026-03-07T14:05:09.0000000
DateTimeOffset: 2026-03-07T14:05:09.0000000+00:00

The DateTimeOffset got its offset, +00:00: the guide says that All DateTimeOffset values are also represented in this format, as an offset from UTC. The DateTime made without a kind is Unspecified, as the constructor's reference says: The Kind property is initialized to Unspecified.

Conclusion​

Pick a standard format by who reads the text. Use O, s, u or R for files, logs and other programs: they come out the same in every culture. Use d, D and the other culture-dependent letters for people, with the culture passed in. Write it with ToString("O"), or as {value:O} inside an interpolated string.

Additional Resources​