DateTime.TryParseExact不能使用预期的字符串 [英] DateTime.TryParseExact not working with expected string

查看:597
本文介绍了DateTime.TryParseExact不能使用预期的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下方法,我传递的值为07 Jan 2014 13:48:46,从我所了解的TryParseExact应该匹配格式dd MMM yyyy hh:mm:ss和返回true,但是它返回false,任何想法?

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?

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小时的时间。

24-hour time requires use of HH, not hh. Lowercase h is for 12 hour time.

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

这篇关于DateTime.TryParseExact不能使用预期的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆