Useful patterns for DateTime.ToString()

Custom DateTime.ToString() formats

d day of the month as a number from 1 through 31 – a single-digit day is formatted without a leading zero
dd day of the month as a number from 01 through 31 – a single-digit day is formatted with a leading zero
ddd abbreviated name of the day of the week (Mon, Tues, Wed etc)
dddd full name of the day of the week (Monday, Tuesday etc)
h 12-hour clock hour (e.g. 7)
hh 12-hour clock, with a leading 0 (e.g. 07)
H 24-hour clock hour (e.g. 19)
HH 24-hour clock hour, with a leading 0 (e.g. 19)
m minutes
mm minutes with a leading zero
M month number
MM month number with leading zero
MMM abbreviated Month Name (e.g. Dec)
MMMM full month name (e.g. December)
s seconds
ss seconds with leading zero
t abbreviated AM / PM (e.g. A or P)
tt AM / PM (e.g. AM or PM
y year, no leading zero (e.g. 2001 would be 1)
yy year, leadin zero (e.g. 2001 would be 01)
yyy year, (e.g. 2001 would be 2001)
yyyy year, (e.g. 2001 would be 2001)
K time zone information of a date and time value (e.g. +05:00)
z with DateTime values, represents the signed offset of the local operating system’s time zone from Coordinated Universal Time (UTC), measured in hours. (e.g. +6)
zz as z but with leadin zero (e.g. +06)
zzz with DateTime values, represents the signed offset of the local operating system’s time zone from UTC, measured in hours and minutes. (e.g. +06:00)
f most significant digit of the seconds fraction; that is, it represents the tenths of a second in a date and time value.
ff two most significant digits of the seconds fraction; that is, it represents the hundredths of a second in a date and time value
fff three most significant digits of the seconds fraction; that is, it represents the milliseconds in a date and time value
ffff four most significant digits of the seconds fraction; that is, it represents the ten thousandths of a second in a date and time value
fffff five most significant digits of the seconds fraction; that is, it represents the hundred thousandths of a second in a date and time value
ffffff six most significant digits of the seconds fraction; that is, it represents the millionths of a second in a date and time value
the seven most significant digits of the seconds fraction; that is, it represents the ten millionths of a second in a date and time value
F most significant digit of the seconds fraction; that is, it represents the tenths of a second in a date and time value. Nothing is displayed if the digit is zero.
: the time separator defined in the current DateTimeFormatInfo..::.TimeSeparator property. This separator is used to differentiate hours, minutes, and seconds.
/ date separator defined in the current DateTimeFormatInfo..::.DateSeparator property. This separator is used to differentiate years, months, and days.
quoted string (quotation mark). Displays the literal value of any string between two quotation marks (“). Your application should precede each quotation mark with an escape character (\).
quoted string (apostrophe). Displays the literal value of any string between two apostrophe (‘) characters.
%c the result associated with a c custom format specifier, when the custom date and time format string consists solely of that custom format specifier. That is, to use the d, f, F, h, m, s, t, y, z, H, or M custom format specifier by itself, the application should specify %d, %f, %F, %h, %m, %s, %t, %y, %z, %H, or %M. For more information about using a single format specifier, see Using Single Custom Format Specifiers.

The patterns for DateTime.ToString ( ‘d’ ) :

0 MM/dd/yyyy 08/22/2006

The patterns for DateTime.ToString ( ‘D’ ) :

0 dddd, dd MMMM yyyy Tuesday, 22 August 2006

The patterns for DateTime.ToString ( ‘f’ ) :

0 dddd, dd MMMM yyyy HH:mm Tuesday, 22 August 2006 06:30
1 dddd, dd MMMM yyyy hh:mm tt Tuesday, 22 August 2006 06:30 AM
2 dddd, dd MMMM yyyy H:mm Tuesday, 22 August 2006 6:30
3 dddd, dd MMMM yyyy h:mm tt Tuesday, 22 August 2006 6:30 AM

The patterns for DateTime.ToString ( ‘F’ ) :

0 dddd, dd MMMM yyyy HH:mm:ss Tuesday, 22 August 2006 06:30:07

The patterns for DateTime.ToString ( ‘g’ ) :

0 MM/dd/yyyy HH:mm 08/22/2006 06:30
1 MM/dd/yyyy hh:mm tt 08/22/2006 06:30 AM
2 MM/dd/yyyy H:mm 08/22/2006 6:30
3 MM/dd/yyyy h:mm tt 08/22/2006 6:30 AM

The patterns for DateTime.ToString ( ‘G’ ) :

0 MM/dd/yyyy HH:mm:ss 08/22/2006 06:30:07

The patterns for DateTime.ToString ( ‘m’ ) :

0 MMMM dd August 22

The patterns for DateTime.ToString ( ‘r’ ) :

0 ddd, dd MMM yyyy HH’:’mm’:’ss ‘GMT’ Tue, 22 Aug 2006 06:30:07 GMT

The patterns for DateTime.ToString ( ‘s’ ) :

0 yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss 2006-08-22T06:30:07

The patterns for DateTime.ToString ( ‘u’ ) :

0 yyyy’-‘MM’-‘dd HH’:’mm’:’ss’Z’ 2006-08-22 06:30:07Z

The patterns for DateTime.ToString ( ‘U’ ) :

0 dddd, dd MMMM yyyy HH:mm:ss Tuesday, 22 August 2006 06:30:07

The patterns for DateTime.ToString ( ‘y’ ) :

0 yyyy MMMM 2006 August

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.