C # DateTime date formatting,
In C #, DateTime is a type that contains Date and time. When ToString () is used to convert a data type to a string, you can convert it to multiple string formats based on the parameters passed to Tostring.
Directory
1. Category
2. Standard Type
3. Custom format type
1. Category
DateTime calls ToString ()Input parametersIt can be divided into two types: Standard and custom:
1) Standard: the system comes with a specific transferSingle CharacterIt can be converted to the format set by the system.
2) custom: freely combine date codes (y, M, d, h, m, s, and f) to display rich date formats.
2. Standard Type
Description: Transfer specificSingle CharacterIt can be converted to the format set by the system.
2.1 symbol comparison table
. Net version4.0
System Version: Win7
Symbol |
Syntax |
Example (13: 09: 55: 2350) |
Y |
DateTime. Now. ToString () |
13:09:55 |
D |
DateTime. Now. ToString ("D") |
|
D |
DateTime. Now. ToString ("D") |
May 9, 2016 |
F |
DateTime. Now. ToString ("F") |
May 9, 2016 |
F |
DateTime. Now. ToString ("F") |
May 9, 2016 13:09:55 |
G |
DateTime. Now. ToString ("G") |
|
G |
DateTime. Now. ToString ("G") |
13:09:55 |
T |
DateTime. Now. ToString ("T") |
|
T |
DateTime. Now. ToString ("T") |
13:09:55 |
U |
DateTime. Now. ToString ("U") |
2016-05-09 13: 09: 55Z |
U |
DateTime. Now. ToString ("U") |
May 9, 2016 5:09:55 |
M |
DateTime. Now. ToString ("M") |
May 9 |
M |
DateTime. Now. ToString ("M") |
May 9 |
R |
DateTime. Now. ToString ("R") |
Mon, 09 May 2016 13:09:55 GMT |
R |
DateTime. Now. ToString ("R") |
Mon, 09 May 2016 13:09:55 GMT |
Y |
DateTime. Now. ToString ("Y") |
May 2016 |
Y |
DateTime. Now. ToString ("Y") |
May 2016 |
O |
DateTime. Now. ToString ("O") |
2016-05-09T13: 09: 55.2350000 |
O |
DateTime. Now. ToString ("O") |
2016-05-09T13: 09: 55.2350000 |
S |
DateTime. Now. ToString ("S") |
2016-05-09T13: 09: 55 |
2.2 example
2.3 pay attention to the system version
'-'Symbol substitution'/'Symbol.
3. Custom format type
Developers can use English characters (y, M, d, h, m, s, f) to replace (year, month, day, hour, minute, second, millisecond) to freely combine the date and time formats.
3.1 symbol comparison table
. Net version4.0
System Version: Win7
Symbol |
Description
|
Syntax |
Example (13: 09: 55: 2350) |
Yy |
The last two digits of the year |
DateTime. Now. ToString ("Yy") |
DateTime. Now. ToString ("yy"); // => 16 |
Yyyy |
4-digit year |
DateTime. Now. ToString ("Yyyy") |
DateTime. Now. ToString ("yyyy"); // => 2016 |
MM |
Two months; fill in 0 before the singular month |
DateTime. Now. ToString ("MM") |
DateTime. Now. ToString ("MM"); // => 05 |
Dd |
Days |
DateTime. Now. ToString ("Dd") |
DateTime. Now. ToString ("dd"); // => 09 |
Ddd |
Week |
DateTime. Now. ToString ("Ddd") |
DateTime. Now. ToString ("ddd"); // => Monday |
Dddd |
Day of the week |
DateTime. Now. ToString ("Dddd") |
DateTime. Now. ToString ("dddd"); // => Monday |
Hh |
Hours in 12-hour format |
DateTime. Now. ToString ("Hh") |
DateTime. Now. ToString ("hh"); // => 01 |
HH |
Hours in the 24-hour format |
DateTime. Now. ToString ("HH") |
DateTime. Now. ToString ("HH"); // => 13 |
Mm |
Minutes |
DateTime. Now. ToString ("Mm") |
DateTime. Now. ToString ("mm"); // => 09 |
Ss |
Seconds |
DateTime. Now. ToString ("Ss") |
DateTime. Now. ToString ("ss"); // => 55 |
Ff |
The first two digits in milliseconds |
DateTime. Now. ToString ("Ff") |
DateTime. Now. ToString ("ff"); // => 23 |
Fff |
The first three digits in milliseconds |
DateTime. Now. ToString ("Fff") |
DateTime. Now. ToString ("fff"); // = & gt; 235 |
Ffff |
The first four digits in milliseconds |
DateTime. Now. ToString ("Ffff") |
DateTime. Now. ToString ("ffff"); // = & gt; 2350 |
Delimiter
|
You can use delimiters to separate year, month, day, hour, minute, and second. The value can be non-key characters such as-,/, and :. |
DateTime. Now. ToString ("yyyy-MM-dd HH: mm: ss: ffff"); // => 2016-05-09 13: 09: 55: 2350 DateTime. Now. ToString ("yyyy/MM/dd HH: mm: ss: ffff"); // => 2016/05/09 13: 09: 55: 2350 DateTime. Now. ToString ("yyyy/MM/dd HH: mm: ss: ffff dddd"); // => 2016/05/09 13: 09: 55: 2350 Monday |
3.2 Example
====================================== Series of articles ==============================================
This article: 1.3 C # DateTime date formatting
C # Article navigation