Original article: http://blog.sina.com.cn/s/blog_4f3247900100alqj.html
The datetime. tostring () function has four overloads. Generally, it is the one with no parameters. It is unknown that datetime. tostring (string format) is more powerful and can output dates of different formats. The following lists some cases for your reference. Some of them are not listed on msdn.
1. y indicates the year. Note that Y in lower case does not represent the year.
2. M indicates the month.
3. d indicates the date. Note that D does not represent anything.
4. h or h indicates the hour, h indicates the 12-hour system, and h indicates the 24-hour system.
5. M indicates minutes.
6. s indicates the second. Note that s does not mean anything.
Format |
Output |
Example |
Year |
Y |
7 |
String YY = datetime. Now. tostring ("Y-mm ") YY = "7-05" |
YY |
07 |
String YY = datetime. Now. tostring ("YY-mm ") YY = "07-05" |
YYY or more y |
1984 |
String YY = datetime. Now. tostring ("YYYY "); YY = "2007" |
Month |
M |
5. |
String MON = datetime. parse ("1984-05-09") tostring ("yyyy-M ") MON = "1984-5" |
Mm |
05. |
String MON = datetime. parse ("1984-05-09") tostring ("mm ") MON = "05" |
Mmm |
If it is a Chinese version of the operating system, the output will be: May. For an English operating system, enter the abbreviation of the first three letters of the Month: May |
String MON = datetime. parse ("2006-07-01"). tostring ("mmm ") English version: Jul Chinese version Operating System: July |
Mmmm or more m |
If it is a Chinese version of the operating system, the output will be: May. For an English operating system, enter the full number of months. |
String MON = datetime. parse ("2006-07-01"). tostring ("mmm ") English version: July Chinese version Operating System: July |
Date or week |
D |
9 |
String dd = datetime. parse ("1984-05-09") tostring ("D ") Dd = "9" |
Dd |
09 |
String dd = datetime. parse ("1984-05-09") tostring ("DD ") Dd = "09" |
Ddd |
For a Chinese operating system, the week, for example, Wednesday, is displayed .. For an English operating system, the abbreviation of the week is output: Wed |
String dd = datetime. parse ("2006-07-01"). tostring ("DDD ") English version: Wed Chinese operating system: Wednesday |
Dddd or more d |
For a Chinese operating system, the week, for example, Wednesday, is displayed .. For an English operating system, the week is output, as shown in figure Wednesday |
String dd = datetime. parse ("2006-07-01"). tostring ("dddd ") English version: Wednesday Chinese operating system: Wednesday |
Hours |
H |
Hour range: 1-12 |
String HH = datetime. Now. tostring ("H "); HH = 8 |
HH or more H |
Hour range: 1-12 |
String HH = datetime. Now. tostring ("hh "); HH = 08 |
H |
Hour range: 0-23 |
String HH = datetime. Now. tostring ("yyyy-H "); HH = 2006-8 |
HH or more H |
Hour range: 0-23 |
String HH = datetime. Now. tostring ("yyyy-hh "); HH = 2006-08 String HH = datetime. Pare ("18:00:00"). tostring ("yyyy-hh "); HH = 2006-18 |
Minutes |
M |
6 |
String Mm = datetime. Now. tostring ("yyyy-mm-dd-M "); Mm = "2006-07-01-6 "; |
Mm or more m |
06 |
String Mm = datetime. Now. tostring ("yyyy-mm-dd-mm "); Mm = "2006-07-01-06 "; |
Seconds |
S |
6 |
String Mm = datetime. Now. tostring ("yyyy-mm-dd-s "); Mm = "2006-07-01-6 "; |
SS or more s |
06 |
String Mm = datetime. Now. tostring ("yyyy-mm-dd-SS "); Mm = "2006-07-01-06 "; |