C# DateTime.ToString() Usage Demonstration
时间:2010-08-20 来源:yuzhangqi
Formatting of Date and Time is an routine in application devlopment. In this blog I would show you how to format a DateTime value using DataTime.ToString() method in a specific culture.
1. Format Pattern
The following table lists the standard DateTime format patterns associated with DateTimeFormatInfo properties. For more information, see Standard Date and Time Format Strings.
The following table lists the custom DateTime format patterns and their behavior. For more information, see Custom Date and Time Format Strings.
Only format patterns listed in the second table above can be used to create custom patterns. Standard format patterns listed in the first table are used only to create standard patterns. Custom patterns are at least two characters long, for example:
-
DateTime.ToString("d") returns the DateTime value; "d" is the standard short date pattern.
-
DateTime.ToString("%d") returns the day of the month; "%d" is a custom pattern.
-
DateTime.ToString("d ") returns the day of the month followed by a white-space character; "d " is a custom pattern.
2. Examples
The following are examples for DateTime.ToString() with diffrent format string and "en-US" culture.
time.ToString() : 8/20/2010 1:58:03 PMtime.ToFileTime() : 129267574834505675
time.ToFileTimeUtc() : 129267574834505675
time.ToLocalTime() : 2010/8/20 13:58:03
time.ToLongDateString() : 2010年8月20日
time.ToLongTimeString() : 13:58:03
time.ToOADate() : 40410.581984375
time.ToShortDateString() : 2010/8/20
time.ToShortTimeString() : 13:58
time.ToUniversalTime() : 2010/8/20 5:58:03
time.Date : 2010/8/20 0:00:00
time.DayOfWeek : Friday
time.DayOfYear : 232
time.Year : 2010
time.Month : 8
time.Day : 20
time.ToString("s") : 2010-08-20T13:58:03
time.ToString("t") : 1:58 PM
time.ToString("T") : 1:58:03 PM
time.ToString("y") : August, 2010
time.ToString("Y") : August, 2010
time.ToString("d") : 8/20/2010
time.ToString("D") : Friday, August 20, 2010
time.ToString("f") : Friday, August 20, 2010 1:58 PM
time.ToString("F") : Friday, August 20, 2010 1:58:03 PM
time.ToString("g") : 8/20/2010 1:58 PM
time.ToString("G") : 8/20/2010 1:58:03 PM
time.ToString("m") : August 20
time.ToString("M") : August 20
time.ToString("u") : 2010-08-20 13:58:03Z
time.ToString("U") : Friday, August 20, 2010 5:58:03 AM
time.ToString("r") : Fri, 20 Aug 2010 13:58:03 GMT
time.ToString("R") : Fri, 20 Aug 2010 13:58:03 GMT
time.ToString("o") : 2010-08-20T13:58:03.4505675+08:00
time.ToString("O") : 2010-08-20T13:58:03.4505675+08:00
time.ToString("%d") : 20
time.ToString("d ") : 20
time.ToString("dd") : 20
time.ToString("ddd") : Fri
time.ToString("dddd") : Friday
time.ToString("%h") : 1
time.ToString("hh") : 01
time.ToString("%H") : 13
time.ToString("HH") : 13
time.ToString("%m") : 58
time.ToString("mm") : 58
time.ToString("%M") : 8
time.ToString("MM") : 08
time.ToString("MMM") : Aug
time.ToString("MMMM") : August
time.ToString("%s") : 3
time.ToString("ss") : 03
time.ToString("%t") : P
time.ToString("tt") : PM
time.ToString("%y") : 10
time.ToString("yy") : 10
time.ToString("yyy") : 2010
time.ToString("yyyy") : 2010
Days interval between 2010-8-19 and 2010-8-20 : 1
Download Source Code
references:
http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.aspx