DateTime.TryParseExact无法使用预期的字符串

嗨我有以下方法,我传递的值为“2014年1月7日13:48:46”,据我所知,TryParseExact应匹配格式“dd MMM yyyy hh:mm:ss”并返回true,然而它回归虚假,任何想法?

string[] formats= {"dd-MM-yyyy hh:mm:ss", "dd MMM yyyy hh:mm:ss", "dd MMM yyyy", "hh-mm-ss", "dd-MM-yyyy", "dd-MM-yy", }; DateTime result; if (DateTime.TryParseExact(value, formats, CultureInfo.CurrentCulture, DateTimeStyles.None, out result)) { return result; } return null; 

24小时的时间需要使用HH ,而不是hh 小写h是12小时的时间。

请参阅: http : //msdn.microsoft.com/en-us/library/8kb3ddd4%28v=vs.110%29.aspx

因为你的格式都没有24小时制

hh说明符是0112 。 它没有13小时。

使用HH说明符代替0023

有关更多信息,请查看;

  • Custom Date and Time Format Strings