无法使用ParseExact解析自定义日期字符串

时间:2016-07-21 11:04:19

标签: c# date-parsing

我遇到了与DateTime.ParseExact相关的问题。无法解析月份的1位数字符串。

//this works fine with 2 digits for month
DateTime.TryParseExact("030405", "MMddyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out date);

//this works also fine with 1 digit for month but with hyphen or space
DateTime.TryParseExact("3-04-05", "M-dd-yy", CultureInfo.InvariantCulture, DateTimeStyles.None, out date);
DateTime.TryParseExact("3 04 05", "M dd yy", CultureInfo.InvariantCulture, DateTimeStyles.None, out date);

//but this DOES NOT work with 1 digit without hyphen
DateTime.TryParseExact("30405", "Mddyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out date);

对我而言看起来不合逻辑。

0 个答案:

没有答案