C # Date formatting

Source: Internet
Author: User

System formatting

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

Custom formatting

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

C # Date formatting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.