简体   繁体   English

DateTime.TryParseExact 不适用于预期的字符串

[英]DateTime.TryParseExact not working with expected string

Hi I have the following method and I'm passing it the value "07 Jan 2014 13:48:46" and from what I understand the TryParseExact should be matching the format "dd MMM yyyy hh:mm:ss" and returning true, however it is returning false, any ideas?嗨,我有以下方法,我将值“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-hour time requires use of HH , not hh . 24 小时制需要使用HH ,而不是hh Lowercase h is for 12 hour time.小写的h表示 12 小时的时间。

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

Because none of your format have 24-hour clock .因为您的格式都没有24 小时制

hh specifier is for 01 to 12 . hh说明符用于0112 It doesn't have 13 as an hour.一小时没有13点。

Use HH specifier instead which is for 00 to 23 .使用HH说明符代替0023

For more information, take a look at;有关更多信息,请查看;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM