C#强转DateTime类型
C#强转DateTime类型
方式一:Convert.ToDateTime(string)
注意:string格式有要求,必须是yyyy-MM-dd hh:mm:ss
方式二:Convert.ToDateTime(string, IFormatProvider)
DateTimeFormatInfo dtFormat = new System.GlobalizationDateTimeFormatInfo(); dtFormat.ShortDatePattern = "yyyy/MM/dd"; DateTime dt = Convert.ToDateTime("2022/10/01", dtFormat);
方式三:DateTime.ParseExact()
string dateString = "20221001"; DateTime dt = DateTime.ParseExact(dateString, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);