This example shows how to format DateTime using String.Format method. All formatting can is done also using datetime.tostring method.
Custom DateTime Formatting
There is following custom format specifiers (year), (month), (Day y
M
d
), h
(Hour), H
(Hour 24), c5/> (minute), s
(second), ( f
second fraction), ( F
second fraction, trailing zeroes is trimmed), t
(p.m. or a.m.) and z
(time zone).
Following examples demonstrate how is the format specifiers rewritten to the output.
//Create date Time 2008-03-09 16:05:07.123DateTime dt =NewDateTime ( -,3,9, -,5,7,123); String.Format ("{0:y yy yyy yyyy}", DT);//"8 008" YearString.Format ("{0:m MM MMM MMMM}", DT);//"3 Mar March" MonthString.Format ("{0:d dd ddd dddd}", DT);//"9 Sun Sunday" DayString.Format ("{0:h hh h hh}", DT);//"4" Hour 12/24String.Format ("{0:m mm}", DT);//"5" MinuteString.Format ("{0:s SS}", DT);//"7" secondString.Format ("{0:f ff FFF FFFF}", DT);//"1 123 1230" Sec.fractionString.Format ("{0:f FF FFF FFFF}", DT);//"1 123 123" without zeroesString.Format ("{0:t tt}", DT);//"P PM" a.m. or p.m.String.Format ("{0:z zz zzz}", DT);//" -6-06-06:00" time zone
can use also Date Separator /
(slash) and time sepatator :
(colon). These characters'll be rewritten to characters defined in the current datetimeformatinfo.dateseparator and< /c12>datetimeformatinfo.timeseparator.
// Date Separator In German culture is "." (So "/" changes to ".") String.Format ("{0:d/m/yyyy HH:mm:ss}"// "9/3/2008 16:05:07"-中文版 (en-US )String.Format ("{0:d/m/yyyy HH:mm:ss}"// "9.3.2008 16:05:07"- German (De-de)
Here is some examples of custom date and time formatting:
//Month/day numbers without/with leading zeroesString.Format ("{0:m/d/yyyy}", DT);//"3/9/2008"String.Format ("{0:mm/dd/yyyy}", DT);//"03/09/2008"//day/month NamesString.Format ("{0:ddd, MMM D, yyyy}", DT);//"Sun, Mar 9,"String.Format ("{0:dddd, MMMM D, yyyy}", DT);//"Sunday, March 9,"//two/four Digit yearString.Format ("{0:MM/DD/YY}", DT);//"03/09/08"String.Format ("{0:mm/dd/yyyy}", DT);//"03/09/2008"
Standard DateTime formatting
In DateTimeFormatInfo there is defined standard patterns for the current culture. For example property Shorttimepattern is a string that contains value for h:mm tt
en -US culture and value for HH:mm
C9>de-de culture.
Following table shows patterns defined in DateTimeFormatInfo and their values for en-US culture. First column contains format specifiers for the String.Format method.
specifier |
DateTimeFormatInfo | Property
Pattern value (for en-US culture) |
t |
Shorttimepattern |
h:mm tt |
d |
ShortDatePattern |
M/d/yyyy |
T |
Longtimepattern |
h:mm:ss tt |
D |
Longdatepattern |
dddd, MMMM dd, yyyy |
f |
(Combination of D and t ) |
dddd, MMMM dd, yyyy h:mm tt |
F |
Fulldatetimepattern |
dddd, MMMM dd, yyyy h:mm:ss tt |
g |
(Combination of d and t ) |
M/d/yyyy h:mm tt |
G |
(Combination of d and T ) |
M/d/yyyy h:mm:ss tt |
m ,M |
Monthdaypattern |
MMMM dd |
y ,Y |
Yearmonthpattern |
MMMM, yyyy |
r ,R |
Rfc1123pattern |
ddd, dd MMM yyyy HH‘:‘mm‘:‘ss ‘GMT‘ (*) |
s |
Sortabledatetimepattern |
yyyy‘-‘MM‘-‘dd‘T‘HH‘:‘mm‘:‘ss (*) |
u |
Universalsortabledatetimepattern |
yyyy‘-‘MM‘-‘dd HH‘:‘mm‘:‘ss‘Z‘ (*) |
|
|
(*) = Culture Independent |
Following examples show usage of standard format specifiers in String.Format method and the resulting output.
String.Format ("{0:t}", DT);//"4:05 PM" ShorttimeString.Format ("{0:D}", DT);//"3/9/2008" ShortDateString.Format ("{0:t}", DT);//"4:05:07 PM" longtimeString.Format ("{0:D}", DT);//"Sunday, March," LongdateString.Format ("{0:f}", DT);//"Sunday, March, 4:05 PM" Longdate+shorttimeString.Format ("{0:f}", DT);//"Sunday, March, 4:05:07 PM" FulldatetimeString.Format ("{0:g}", DT);//"3/9/2008 4:05 PM" Shortdate+shorttimeString.Format ("{0:g}", DT);//"3/9/2008 4:05:07 PM" Shortdate+longtimeString.Format ("{0:m}", DT);//"March" MonthDayString.Format ("{0:y}", DT);//"March," YearmonthString.Format ("{0:r}", DT);//"Sun, 16:05:07 GMT" RFC1123String.Format ("{0:s}", DT);//"2008-03-09t16:05:07" SortabledatetimeString.Format ("{0:u}", DT);//"2008-03-09 16:05:07z" Universalsortabledatetime
String Format for DateTime