c#中的日期格式异常无效 [英] Invalid date format exception in c#
本文介绍了c#中的日期格式异常无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的要求是编写一个可以接受两种日期格式的代码,即yyyymmddhhmmss和yyyymmdd。我已经编写了代码,但是它的工作时间很少,但并非适用于所有。
My requirement was to write a code that can accept two type of date format i.e yyyymmddhhmmss and yyyymmdd. I have written the code but it is working for few date but not for all.
string[] formats = { "yyyymmdd","yyyymmddhhmmss" };
bool valid = true;
if (!DateTime.TryParseExact(DesiredReportVersionDate, formats, new CultureInfo("en-US"), DateTimeStyles.None, out timevalue))
{
valid = false;
throw new Exception("invalid format" + timevalue);
}
但是,通过19901212033047价值是例外。根据我的理解,它应该是有效的。
请帮忙。
提前谢谢
However,passing 19901212033047 value is giving exception.It should be valid as per my understanding.
Please help.
Thanks in advance
推荐答案
问题应该是你使用的m(小m)也是月份和分钟。
它应该是M(大写M)月份和m(小m)分钟......
The problem should be that you using m (small m) for month and minute also.
It should be M (capital M) for month and m (small m) for minute...
string[] formats = { "yyyyMMdd","yyyyMMddhhmmss" };
更改格式字符串
Change the format strings from
string[] formats = { "yyyymmdd","yyyymmddhhmmss" };
bool valid = true;
to
to
string[] formats = { "yyyyMMdd","yyyyMMddhhmmss" };
bool valid = true;
MM =月
mm =分钟。
如果你想24,请用HH而不是hh小时时间。
MM = month
mm = minutes.
use HH instead of hh if you want 24 hour time.
我从一些帮助中得到了这个。
使用这个yyyyMMddHHmmss。因为它是区分大小的
谢谢
I got this from some help.
use this yyyyMMddHHmmss. Because it's case sensitive
Thanks
这篇关于c#中的日期格式异常无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文