C# custom format tokens.

Letter-repetition tokens in the CLDR style, with one trap all of its own: a single-character format string is a STANDARD format, not a custom token — "d" alone means the short-date pattern and "%d" (or any second character) is needed to get the day number. Month and weekday names follow the culture passed to ToString. For serialization, prefer the K specifier: it renders Z for UTC, the offset for local times, and nothing for unspecified kinds, which is what round-tripping needs. There are no tokens for day of year, week numbers (use the ISOWeek class), quarters, unix timestamps, or time-zone names — only offsets.

TokenMeansExample
yyyyFour-digit year2006
yyTwo-digit year

A single y gives the unpadded 0–99 year; yyy and up pad to that many digits.

06
MMonth number

As a complete format string, "M" is the standard month/day pattern — write "%M".

1
MMMonth number, padded01
MMMMonth name, abbreviated

Culture-sensitive: Jun in en-US, juin in fr-FR.

Jan
MMMMMonth name, fullJanuary
dDay of month

As a complete format string, "d" is the standard short-date pattern — write "%d".

2
ddDay of month, padded02
dddWeekday, abbreviatedMon
ddddWeekday, fullMonday
HHour, 24-hour clock

Alone it must be written "%H" to avoid the standard-format lookup.

15
HHHour, 24-hour clock, padded15
hHour, 12-hour clock

Alone it must be written "%h".

3
hhHour, 12-hour clock, padded03
ttAM/PM marker

A single t prints only the first character (P) of the designator.

PM
mmMinute, padded

A bare m (as "%m") gives the unpadded minute.

04
ssSecond, padded

A bare s (as "%s") gives the unpadded second.

05
fffFractional seconds

Each f is one digit, up to fffffff (100 ns); uppercase F trims trailing zeros instead of printing them.

000
zzzUTC offset, extended

zz gives hours only (-07), z unpadded (-7). Prefer K when round-tripping: Z for UTC, empty for unspecified.

-07:00
gEra

Renders A.D.; gg is identical.

AD

Examples render the reference instant, Mon, Jan 2 2006, 3:04:05 PM MST.

Literal text

Backslash escapes one character (\h); longer runs go in 'single' or "double" quotes. Beware that : and / are culture-dependent separator specifiers, not literals — escape them for a fixed output.

Verified against .NET docs — custom date and time format strings.

Translate C# custom format