A format specifier can quickly convert a DateTime object into a properly formatted value string:
DateTime dateOutput = new DateTime( 2001, 1, 1 ); Console.WriteLine( "{0:d} [" + string.Format( "{0:d}", dateOutput ) + "]" );
Table C-2 describes how to format dates that take a DateTime object of January 1, 2001.
SPECIFIER | TYPE | FORMAT | OUTPUT |
---|---|---|---|
d | Short date | {0:d} | 1/1/2001 |
dd | Day | {0:dd} | 01 |
ddd | Day name | {0:ddd} | Mon |
dddd | Full day name | {0:dddd} | Monday |
D | Long date | {0:D} | Monday, January 01, 2001 |
t | Short time | {0:t} | 12:00 AM |
tt | A.M. or P.M. | {0:tt} | AM |
T | Long time | {0:T} | 12:00:00 AM |
f | Full date and time | {0:f} | Monday, January 01, 2001 12:00 AM |
F | Full date and time (long) | {0:F} | Monday, January 01, 2001 12:00:00 AM |
g | Default date and time | {0:g} | 1/1/2001 12:00 AM |
G | Default date and time (long) | {0:G} | 1/1/2001 12:00:00 AM |
mm | Minute 00 “59 | {0:mm} | 00 |
M | Month/day pattern | {0:M} | January 01 |
MM | Month 01 “12 | {0:MM} | 01 |
MMM | Month abbreviation | {0:MMM} | Jan |
MMMM | Full month name | {0:MMMM} | January |
r | RFC 1123 date string | {0:r} | Mon, 01 Jan 2001 00:00:00 GMT |
s | Sortable date string | {0:s} | 2001-01-01T00:00:00 |
ss | Seconds 00 “59 | {0:ss} | 00 |
u | Universal sortable, local time | {0:u} | 2001-01-01 00:00:00Z |
U | Universal sortable, Greenwich Mean Time (GMT) | {0:U} | Monday, January 01, 2001 8:00:00 AM |
Y | Year/month pattern | {0:Y} | January, 2001 |
f, ff, ... | Second fractions | {0:fff} | 000 |
gg, ... | Era | {0:gg} | A.D. |
fh | Two-digit hour | {0:hh} | 12 |
HH | Two-digit hour, 24-hour format | {0:HH} | 00 |
yy | Year, two digits | {0:yy} | 01 |
yyyy | Year | {0:yyyy} | 2001 |
: | Separator | {0:hh:mm:ss} | 12:00:00 |
/ | Separator | {0:dd/mm/yyyy} | 01/00/2001 |