Three Methods of Converting String of String Format to DateTime Type in C and. Net
- 2021-07-09 09:04:22
- OfStack
Mode 1: Convert. ToDateTime (string)
Convert.ToDateTime(string)
Note: string format is required and must be yyyy-MM-dd hh: mm: ss
Mode 2: Convert. ToDateTime (string, IFormatProvider)
DateTimeFormatInfo dtFormat = new System.GlobalizationDateTimeFormatInfo();
dtFormat.ShortDatePattern = "yyyy/MM/dd";
DateTime dt = Convert.ToDateTime("2014/10/10", dtFormat);
Description: Rules can be customized in any format.
Mode 3: DateTime. ParseExact ()
string dateString = "20141010";
DateTime dt = DateTime.ParseExact(dateString, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
Rules can be customized in any format, and the effect is the same as Mode 2.