# datetime Date Formatting
In C #, DateTime is a type that contains a date and time that can be converted to multiple string formats based on the arguments passed in to ToString () when this type is converted to a string by ToString ().
Directory
1. Classification
2. Format type
3. Custom Format types
1. Classification
DateTime calls to ToString () Pass in parameters that can be divided into standard and custom two types:
1) Format: The system comes with, into a specific single character can be converted to the system has been set.
2) Custom: Free combination of date codes (Y, M, D, H, M, S, f) to display rich date formats.
2. Format type
Note : Transferring to a specific single character can be converted to a format that is already set by the system.
2.1 Format Source
The date and time format can be set in the Regional and Language (location: Control Panel → regional and language) options in Windows systems.
The categories that can be set are: Short date, long date, short time, long time, etc.
When you call ToString () for conversion, many of the conversions are made by combining the 4 categories above.
2.2 Symbol Comparison 2.2.1 Environment
. NET version : 4.0
system version : Win7
format Information :
2.2.2 Table
Symbol |
Grammar |
Example (2016-05-09 13:09:55:2350) |
Format description |
Y |
DateTime.Now.ToString () |
2016/5/9 13:09:55 |
Short Date long time |
D |
DateTime.Now.ToString ("D") |
2016/5/9 |
Short Date |
D |
DateTime.Now.ToString ("D") |
May 9, 2016 |
Long Date |
F |
DateTime.Now.ToString ("f") |
May 9, 2016 13:09 |
Long Date short time |
F |
DateTime.Now.ToString ("F") |
May 9, 2016 13:09:55 |
Long Date long time |
G |
DateTime.Now.ToString ("g") |
2016/5/9 13:09 |
Short Date short time |
G |
DateTime.Now.ToString ("G") |
2016/5/9 13:09:55 |
Short Date long time |
T |
DateTime.Now.ToString ("t") |
13:09 |
Short time |
T |
DateTime.Now.ToString ("T") |
13:09:55 |
Long time |
U |
DateTime.Now.ToString ("u") |
2016-05-09 13:09:55z |
|
U |
DateTime.Now.ToString ("U") |
May 9, 2016 5:09:55 |
Long date and long time of the Prime Meridian |
M |
DateTime.Now.ToString ("m") |
May 9 |
|
M |
DateTime.Now.ToString ("M") |
May 9 |
|
R |
DateTime.Now.ToString ("r") |
Mon, 13:09:55 GMT |
|
R |
DateTime.Now.ToString ("R") |
Mon, 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.3 Example Diagram
2.4 Win2003 Version
The '-' symbol replaces the '/' symbol in Win2003, D, G, and G in the default format (short date format: yyyy-m-d).
3. Custom Format types
Developers can freely combine date and time formats with English characters (Y, M, D, H, M, S, f), respectively (year, month, day, hour, minute, second, and millisecond).
3.1 Symbol Comparison Chart
. NET version : 4.0
system version : Win7
Symbol |
Description
|
Grammar |
Example (2016-05-09 13:09:55:2350) |
Yy |
Two digits after the year |
DateTime.Now.ToString ("yy") |
DateTime.Now.ToString ("yy"); = 16 |
yyyy |
4-bit year |
DateTime.Now.ToString ("yyyy") |
DateTime.Now.ToString ("yyyy"); = 2016 |
Mm |
two-digit month; 0 padding in front of singular month |
DateTime.Now.ToString ("MM") |
DateTime.Now.ToString ("MM"); = 05 |
Dd |
Number of days |
DateTime.Now.ToString ("dd") |
DateTime.Now.ToString ("DD"); = 09 |
Ddd |
Weeks |
DateTime.Now.ToString ("ddd") |
DateTime.Now.ToString ("ddd"); = Monday |
dddd |
Day of the Week |
DateTime.Now.ToString ("dddd") |
DateTime.Now.ToString ("dddd"); = Monday |
hh |
Number of hours in a 12-hour system |
DateTime.Now.ToString ("hh") |
DateTime.Now.ToString ("HH"); = 01 |
HH |
Number of hours in a 24-hour system |
DateTime.Now.ToString ("HH") |
DateTime.Now.ToString ("HH"); = 13 |
Mm |
Number of minutes |
DateTime.Now.ToString ("mm") |
DateTime.Now.ToString ("mm"); = 09 |
Ss |
Number of seconds |
DateTime.Now.ToString ("SS") |
DateTime.Now.ToString ("SS"); = 55 |
Ff |
Number of milliseconds first 2 bits |
DateTime.Now.ToString ("ff") |
DateTime.Now.ToString ("FF"); = 23 |
Fff |
Number of milliseconds first 3 bits |
DateTime.Now.ToString ("FFF") |
DateTime.Now.ToString ("fff"); = 235 |
Ffff |
Number of milliseconds first 4 bits |
DateTime.Now.ToString ("ffff") |
DateTime.Now.ToString ("FFFF"); = 2350 |
Separator
|
You can use separators to separate the days of the month and seconds. Contains values that are:-,/,: etc. non-key characters |
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 Diagram
Https://www.cnblogs.com/polk6/p/5465088.html
C # date format