1. String conversion to date 1.1 the first way
Using the convert.todatetime method, this method has many overloaded methods, here I introduce two common overloaded methods.
The first type:
Use:
Convert.todatetime (string)
there is a limit to the conversion of this method, that is, the format of the string should resemble "Yyyy-mm-dd Hh:mm:ss", for example: "2017-12-12 00:01:11". Why did you say that it should be similar to the "YYYY-MM-DD hh:mm:ss" format, because it can also be recognized like this format "2017/12/12 00:01:11". The method can also convert null values, and NULL will be converted to "1/1/0001 12:00:00", although the method supports a limited number of situations. Using this method, the flexibility is stronger,
The second type:
Convert.todatetime (string, IFormatProvider)
For example:
New"yyyy/mm/dd"= Convert.todatetime ("2011/05/26 ", Dtformat);
1.2 Second Way
Using datetime.parseexact to use this method, you can also achieve the purpose of converting the string to a date, the method also has overloaded methods, here I introduce a:
ParseExact (String, String, IFormatProvider)
The next step is to use the
string " 20110526 " "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
Or:
" YYYYMMDD ", System.Globalization.CultureInfo.InvariantCulture);
2. Date conversion to string 2.1 The first way
If the date needs to be converted to a string, then use the ToString () method directly.
Like what:
DateTime dt == dt. ToString ("yyyy mm month dd hh when mm min ss seconds ");
Some date format identifiers are given below
YYYY: Year of representation
MM: On behalf of the month
DD: Representative Day
HH: Representative hour (24-hour system)
HH: Representative Hour (12-hour system)
MM: Represents minutes
SS: Represents seconds
"C #" Analysis of date processing in C #