拼接字符串的形式
DateTime dateTime=Convert.ToDateTime("20201222".Substring(0,4)+"-"+"20201222".Substring(4,2)+"-"+"20201222".Substring(6,2));
Convert.ToDateTime(string)
Convert.ToDateTime(string) //string格式有要求,必须是yyyy-MM-dd hh:mm:ss
DateTime.ParseExact()
string dateString = "20201222";
DateTime dateTime = DateTime.ParseExact(dateString, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
//或者
DateTime dateTime = DateTime.ParseExact(dateString, "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);
Convert.ToDateTime(string, IFormatProvider)
DateTime dateTime;
DateTimeFormatInfo dtFormat = new System.GlobalizationDateTimeFormatInfo();
dtFormat.ShortDatePattern = "yyyy/MM/dd";
dateTime = Convert.ToDateTime("2012/12/22", dtFormat);
DateTime.TryParse(string,out datetime)
用DateTime.TryParse(string,out datetime)转换后,得到的datetime可以用 datetime.ToString("yyy-MM-dd")来转换为特殊需求的格式