C# 把字符串类型日期转换为日期类型

方法一:Convert.ToDateTime(string)

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

================================================

方法二:Convert.ToDateTime(string, IFormatProvider)

DateTime dt;

DateTimeFormatInfo dtFormat = new System.GlobalizationDateTimeFormatInfo();

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

dt = Convert.ToDateTime("2018/07/16", dtFormat);

================================================

方法三:DateTime.ParseExact()

string dateString = "20180716";

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

或者

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

 

posted @ 2018-07-18 16:59  恋上微笑的天使  阅读(914)  评论(0编辑  收藏  举报