C# string格式的日期时间字符串转为DateTime类型

(1 )Convert.ToDateTime(string)

string格式有要求,必须是yyyy-MM-dd hh:mm:ss

 

(2):Convert.ToDateTime(string, IFormatProvider)

DateTime dt;

DateTimeFormatInfo dtFormat = new System.GlobalizationDateTimeFormatInfo();

dtFormat.ShortDatePattern = "yyyy/MM/dd";

dt = Convert.ToDateTime("2012/11/26", dtFormat);

 

(3):DateTime.ParseExact()

string dateString = "20121126";

DateTime dt = DateTime.ParseExact(dateString, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);

或者

DateTime dt = DateTime.ParseExact(dateString, "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);

原文地址

上一篇:jsp 通过cookie 记住用户名


下一篇:LeetCode: 3_Longest Substring Without Repeating Characters | 求没有重复字符的最长子串的长度 | Medium