DateTime.Parse(string s,DateTimeFormatInfo provider)
帮助上这样写:字符串 s 是使用 DateTimeFormatInfo 对象中的格式设置信息分析的,该信息由 provider 参数显式或隐式提供。 s 参数所包含的日期和时间的表示形式必须使用 provider 所指定的区域性的 DateTimeFormatInfo.GetAllDateTimePatterns()方法所返回的格式之一。下列字符串都能解析成功 
DateTime.Parse("2013-1-2 10:00:00 PM",DateTimeFormatInfo.CurrentInfo);
DateTime.Parse("1/2/2013 10:00:00+07:00", DateTimeFormatInfo.CurrentInfo)
DateTime.Parse("2013年2月1日 10:00:00", DateTimeFormatInfo.InvariantInfo)
但是DateTimeFormatInfo.GetAllDateTimePatterns()返回的格式中没有上面的格式,比如GetAllDateTimePatternse()返回的格式中与第1个最接近的是 "yyyy-M-d tt h:mm:ss",应该是解析
"2013-1-2 PM 10:00:00"这样的字符串,试了一下这个字符串也能解析。
第三个,InvariantInfo中也没有支持中文的格式。另外改变符号也都能解析
DateTimeFormatInfo dtf = (DateTimeFormatInfo)DateTimeFormatInfo.CurrentInfo.Clone();
dtf.DateSeparator = "*";
DateTime dt1 = DateTime.Parse("2013*1*2",dtf);
DateTime dt2 = DateTime.Parse("2013-1-2", dtf); //都能解析对被解析的字符串s的格式的要求由什么规定?虽然帮助上说会尽量忽略无法识别的数据,也不能太随意了。
谢谢!

解决方案 »

  1.   

    如果你确定格式,可以使用DateTime.ParseExact代替。
    DateTime.Parse格式化取决于你的操作系统/语言区域设置支持的日期格式。
      

  2.   

    DateTime.Parse解析不了的,可以用DateTime.ParseExact方法,把那个时间格式字符串写正确就行了。
      

  3.   

    ParseExact我知道。只是不知道Parse什么格式能解析什么不能解析,不知道规则。
    本地区域设置支持的格式应该就是DateTimeFormatInfo.CurrentInfo.GetAllDateTimePatternse()返回的所有格式。