ParseExact一个字符串的DateTime失败 [英] ParseExact a string to DateTime fails

查看:115
本文介绍了ParseExact一个字符串的DateTime失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  11月8日1:44 

当我使用这个代码,它失败:

 字符串DD =11月8日1:44; 


{
DateTime.ParseExact(DD,MMM DD HH:MM,CultureInfo.InvariantCulture);
}
赶上(异常前)
{
MessageBox.Show(糟糕的一天,因为+ ex.Message);
}



它说,



< BLOCKQUOTE>

字符串未被识别为有效的DateTime




请让我知道我错了。


解决方案

这格式是

  MMM深高:毫米

%的的 MSDN 你需要一个 D ^ h 由于这样的事实,你的源字符串使用一个单一的数字日子和时辰。




D< 。/ p>

这个月的一天,从1到31



更多信息:D自定义格式说明符。



2009/6/1下午一点45分30秒 - > 1



1 2009/6/15 :45:30 PM - > 15








DD。



这个月的一天,从01到31



更多信息:DD自定义格式说明符。



2009/6/1下午一点45分30秒 - > 01



1 2009/6/15 :45:30 PM - > 15








。H



小时,采用24小时制,从0到23



详细信息: H自定义格式说明符。



2009/6/15上午1点45分三十秒 - > 1



1 2009/6/15 :45:30 PM - > 13








。HH



小时,采用24小时制,从00到23



详细信息: HH自定义格式说明符。



2009/6/15上午1点45分三十秒 - > 01



1 2009/6/15 :45:30 PM - > 13



Nov 8 1:44

When I use this code, it fails:

string DD = "Nov 8 1:44";

try
{
    DateTime.ParseExact(DD, "MMM dd HH:mm", CultureInfo.InvariantCulture);
}
catch(Exception ex)
{
    MessageBox.Show("Bad day because " + ex.Message);
}

It says,

String was not recognized as a valid DateTime

Please let me know where am I wrong.

解决方案

That format would be

MMM d H:mm

per the MSDN you need a single d and H due to the fact that your source string is using a single digit day and hour.

"d"

The day of the month, from 1 through 31.

More information: The "d" Custom Format Specifier.

6/1/2009 1:45:30 PM -> 1

6/15/2009 1:45:30 PM -> 15


"dd"

The day of the month, from 01 through 31.

More information: The "dd" Custom Format Specifier.

6/1/2009 1:45:30 PM -> 01

6/15/2009 1:45:30 PM -> 15


"H"

The hour, using a 24-hour clock from 0 to 23.

More information: The "H" Custom Format Specifier.

6/15/2009 1:45:30 AM -> 1

6/15/2009 1:45:30 PM -> 13


"HH"

The hour, using a 24-hour clock from 00 to 23.

More information: The "HH" Custom Format Specifier.

6/15/2009 1:45:30 AM -> 01

6/15/2009 1:45:30 PM -> 13

这篇关于ParseExact一个字符串的DateTime失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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