/// <summary> /// 字符串转时间 /// </summary> /// <param name="dateString">时间字符串</param> /// <param name="format">时间格式(美国佬时间时可不设置)</param> /// <param name="isUsType">是否是美国佬时间</param> /// <returns></returns> public static DateTime ConvertDateTime(string dateString, string format, bool isUsType) { DateTime result; if (isUsType) { DateTimeFormatInfo dtfi = new CultureInfo("en-US", false).DateTimeFormat; dtfi.ShortTimePattern = "t"; result = DateTime.Parse(dateString, dtfi); } else { CultureInfo provider = new CultureInfo("fr-FR"); result = DateTime.ParseExact(dateString, format, provider); } return result; }
需要引入命名空间 using System.Globalization;
发现用Convert.ToDateTime(xxxx)报错,就百度了,发现这样就不报错了。