You are on page 1of 3

11/03/2015

StringFormatforDateTime[C#]

Menu:
Homepage
Allexamples
Contactme

StringFormatforDateTime[C#]
ThisexampleshowshowtoformatDateTimeusingString.Formatmethod.Allformattingcanbedonealsousing
DateTime.ToStringmethod.

CustomDateTimeFormatting
Therearefollowingcustomformatspecifiersy(year),M(month),d(day),h(hour12),H(hour24),m(minute),s(second),
f(secondfraction),F(secondfraction,trailingzeroesaretrimmed),t(P.MorA.M)andz(timezone).
Followingexamplesdemonstratehowaretheformatspecifiersrewrittentotheoutput.
[C#]
//createdatetime2008030916:05:07.123
DateTimedt=newDateTime(2008,3,9,16,5,7,123);
String.Format("{0:yyyyyyyyyy}",dt);//"8080082008"year
String.Format("{0:MMMMMMMMMM}",dt);//"303MarMarch"month
String.Format("{0:dddddddddd}",dt);//"909SunSunday"day
String.Format("{0:hhhHHH}",dt);//"4041616"hour12/24
String.Format("{0:mmm}",dt);//"505"minute
String.Format("{0:sss}",dt);//"707"second
String.Format("{0:ffffffffff}",dt);//"1121231230"sec.fraction
String.Format("{0:FFFFFFFFFF}",dt);//"112123123"withoutzeroes
String.Format("{0:ttt}",dt);//"PPM"A.M.orP.M.
String.Format("{0:zzzzzz}",dt);//"60606:00"timezone

Youcanusealsodateseparator/(slash)andtimesepatator:(colon).Thesecharacterswillberewrittentocharacters
definedinthecurrentDateTimeForma
tInfo.DateSepa
ratorandDateTimeForma
tInfo.TimeSepa
rator.
[C#]
//dateseparatoringermancultureis"."(so"/"changesto".")
String.Format("{0:d/M/yyyyHH:mm:ss}",dt);//"9/3/200816:05:07"english(enUS)
String.Format("{0:d/M/yyyyHH:mm:ss}",dt);//"9.3.200816:05:07"german(deDE)

Herearesomeexamplesofcustomdateandtimeformatting:
[C#]
//month/daynumberswithout/withleadingzeroes
String.Format("{0:M/d/yyyy}",dt);//"3/9/2008"
String.Format("{0:MM/dd/yyyy}",dt);//"03/09/2008"
//day/monthnames
String.Format("{0:ddd,MMMd,yyyy}",dt);//"Sun,Mar9,2008"
String.Format("{0:dddd,MMMMd,yyyy}",dt);//"Sunday,March9,2008"
http://www.csharpexamples.net/stringformatdatetime/

1/3

11/03/2015

StringFormatforDateTime[C#]

//two/fourdigityear
String.Format("{0:MM/dd/yy}",dt);//"03/09/08"
String.Format("{0:MM/dd/yyyy}",dt);//"03/09/2008"

StandardDateTimeFormatting
InDateTimeForma
tInfotherearedefinedstandardpatternsforthecurrentculture.Forexampleproperty
ShortTimePatternisstringthatcontainsvalueh:mmttforenUScultureandvalueHH:mmfordeDEculture.
FollowingtableshowspatternsdefinedinDateTimeForma
tInfoandtheirvaluesforenUSculture.Firstcolumncontains
formatspecifiersfortheString.Formatmethod.
Specifier DateTimeFormatInfoproperty
Patternvalue(forenUSculture)
ShortTimePattern
h:mmtt
d
ShortDatePattern
M/d/yyyy
T
LongTimePattern
h:mm:sstt
D
LongDatePattern
dddd,MMMMdd,yyyy
f
(combinationofDandt)
dddd,MMMMdd,yyyyh:mmtt
F
FullDateTimePattern
dddd,MMMMdd,yyyyh:mm:sstt
g
(combinationofdandt)
M/d/yyyyh:mmtt
G
(combinationofdandT)
M/d/yyyyh:mm:sstt
m,M
MonthDayPattern
MMMMdd
y,Y
YearMonthPattern
MMMM,yyyy
r,R
RFC1123Pattern
ddd,ddMMMyyyyHH':'mm':'ss'GMT'(*)
s
SortableDateTi
mePattern
yyyy''MM''dd'T'HH':'mm':'ss(*)
u
UniversalSorta
bleDateTimePat
tern yyyy''MM''ddHH':'mm':'ss'Z'(*)

(*)=cultureindependent
t

FollowingexamplesshowusageofstandardformatspecifiersinString.Formatmethodandtheresultingoutput.
[C#]
String.Format("{0:t}",dt);//"4:05PM"ShortTime
String.Format("{0:d}",dt);//"3/9/2008"ShortDate
String.Format("{0:T}",dt);//"4:05:07PM"LongTime
String.Format("{0:D}",dt);//"Sunday,March09,2008"LongDate
String.Format("{0:f}",dt);//"Sunday,March09,20084:05PM"LongDate+ShortTime
String.Format("{0:F}",dt);//"Sunday,March09,20084:05:07PM"FullDateTime
String.Format("{0:g}",dt);//"3/9/20084:05PM"ShortDate+ShortTime
String.Format("{0:G}",dt);//"3/9/20084:05:07PM"ShortDate+LongTime
String.Format("{0:m}",dt);//"March09"MonthDay
String.Format("{0:y}",dt);//"March,2008"YearMonth
String.Format("{0:r}",dt);//"Sun,09Mar200816:05:07GMT"RFC1123
String.Format("{0:s}",dt);//"20080309T16:05:07"SortableDateTime
String.Format("{0:u}",dt);//"2008030916:05:07Z"UniversalSortableDateTime

Seealso
[C#]StringFormatforDoubleformatfloatnumbers
[C#]StringFormatforIntformat(align)integernumbers
[C#]IFormatProviderforNumbersparsefloatnumberswithIFormatProvider
[C#]CustomIFormatProviderstringformattingwithcustomIFormatProvider
[C#]AlignStringwithSpaceshowtoaligntexttotherightorleft
[C#]IndentStringwithSpaceshowtoindenttextwithrepeatedspaces
CustomDateandTimeFormatStringsMSDNcustomdatetimeformatting
StandardDateandTimeFormatStringsMSDNstandarddatetimeformatting
DateTimeForma
tInfoMSDNdatetimepatternsforspecificcultures
String.FormatMSDNmethodtoformatstrings
http://www.csharpexamples.net/stringformatdatetime/

2/3

11/03/2015

StringFormatforDateTime[C#]

ByJanSlama,25Mar2008
Copyright2010JanSlama.

http://www.csharpexamples.net/stringformatdatetime/

3/3

You might also like