Formatting Dates

Dates can be formatted with a single shortcut character, such as: "G," or a composite of specifiers, such as "HH:mm". If you want to use a single character as a regular specifier and not a shortcut, prefix the string with %. For example: "%m" outputs the unpadded minute instead of the Month+Day.

Table 12-7 Single Character Shortcut Date Formats

Format Description

t

Short Time "4:05 PM"

T

Long Time "4:05:07 PM"

d

Short Date "3/9/2013"

D

Long Date "Friday, March 09, 2013"

f

Long Date + Short Time "Friday, March 09, 2013 4:05 PM"

F

Long Date + Long Time "Friday, March 09, 2013 4:05:07 PM"

g

Short Date + Short Time "3/9/2013 4:05 PM"

G

Short Date + Long Time "3/9/2013 4:05:07 PM" (default)

m

Month + Day "March 09"

y

Month + Year "March, 2013"

r

RFC 1123 "Fri, 09 Mar 2013 16:05:07 GMT"

s

Sortable Date/Time "2013-03-09T16:05:07"

u

Universal Sortable Date/Time "2013-03-09 16:05:07Z"

Table 12-8 Date Format Specifiers (more than one character)

Format Description Examples are for 2013-04-05 04:07:09 PM CST

yy

Year "13"

yyyy

Year "2013"

M

Month "4"

MM

Month "04"

MMM

Month "Apr"

MMMM

Month "April"

d

Day "5"

dd

Day "05"

ddd

Day "Sun"

dddd

Day "Sunday"

h

12-Hour "4"

hh

12-Hour "04"

H

24-Hour "16" (if 4 AM "4")

HH

24-Hour "16" (if 4 AM "04")

m

Minute "7"

MM

Minute "07"

s

Seconds "9"

ss

Seconds "09"

f

Fractions of a second (Can be repeated 1-4 times for more precision)

F

Fractions of a second without trailing zeros (Can be repeated 1-4 times)

t

AM or PM designator "P" (blank for 24-hour only cultures)

tt

AM or PM designator "PM" (blank for 24-hour only cultures)

z

GMT offset "-6"

zz

GMT offset "-06"

zzz

GMT offset "-06:00"

:

Time separator (locale-specific)

/

Date separator (locale-specific)

\<char>

Escape character (<char> is treated as literal output), for example: "{0:HH\h}" outputs "16h"

Any other character

Copied to output unchanged