2014-07-03 19:08:37|?次阅读|上传:wustguangh【已有?条评论】发表评论
本文介绍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);