Home>

In c#datetime is a type containing date and time.When this type is converted to a string by tostring (),Can be converted to multiple string formats based on the parameters passed to tostring ().

In c#datetime is a type containing date and time.When this type is converted to a string by tostring (),Can be converted to multiple string formats based on the parameters passed to tostring ().

Classification

The parameters passed to datetime tostring () can be divided into standard and custom:

1) Standard:the system comes with,By entering a specific single character, it can be converted into the format that the system has set.

2) Custom:Freely combine date codes (y, m, d, h, m, s, f) to display rich date formats

2. Type of system

Note:The specific single character can be converted into the format that the system has set.

2.1 Format Source

The date and time format can be set in the regional and language (location:control panel → regional and language) option in the windows system.

The categories that can be set are:short date, long date, short time, long time, etc.

When calling tostring () for conversion,Many conversion methods are combined through the above 4 classifications.

2.2 Symbol comparison table

2.2.1 Environment

.net version:4.0

System version:win7

Format information:

2.2.2 Comparison table

Symbol Syntax Example (2016-05-09 13:09:55:2350) Format description
y datetime.now.tostring () 5/9/2016 13:09:55 Short date long
d datetime.now.tostring ("d") 5/9/2016 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") 5/9/2016 13:09 Short date short time
g datetime.now.tostring ("g") 5/9/2016 13:09:55 Short date long
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 Prime Meridian
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.3 Example diagram

2.4 win2003 version

In the default characters of win2003, the "/" symbol will be used instead of the "/" symbol in the d, g, and g formats (the short date format is:yyyy-m-d).

3. Custom Format Type

Developers can use English characters (y, m, d, h, m, s, f) instead of (year, month, day, hour, minute, second, millisecond) to freely combine date time format.

3.1 Symbol comparison table

.net version:4.0

System version:win7

Symbol Description

grammar Example (2016-05-09 13:09:55:2350)
yy Last two digits of 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 monthsSingular months are padded with 0 datetime.now.tostring ("mm") datetime.now.tostring ("mm");//=>05
dd Days datetime.now.tostring ("dd") datetime.now.tostring ("dd");//=>09
ddd which day datetime.now.tostring ("ddd") datetime.now.tostring ("ddd");//=>Monday
dddd Day of the week datetime.now.tostring ("dddd") datetime.now.tostring ("dddd");//=>Monday
hh 12-hour hour datetime.now.tostring ("hh") datetime.now.tostring ("hh");//=>01
hh 24-hour hour 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 First 2 digits of milliseconds datetime.now.tostring ("ff") datetime.now.tostring ("ff");//=>23
fff First 3 digits of milliseconds datetime.now.tostring ("fff") datetime.now.tostring ("fff");//=>235
ffff First 4 digits of milliseconds datetime.now.tostring ("ffff") datetime.now.tostring ("ffff");//=>2350
Delimiter

You can use separators to separate the year, month, day, hour, minute, and second.

Contains values ​​can be:-, /,:and other non-critical 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

c
  • Previous Parsing pointers to objects in C ++
  • Next C ++ implementation of common cache algorithms and LRU