如何将字符串转换为c#中的特定DateTime格式? [英] How to convert a string to a specific DateTime format in c#?

查看:139
本文介绍了如何将字符串转换为c#中的特定DateTime格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将特定格式的字符串28/09/2009转换为 DateTime
例如:
我要将2009-09-28 17:30:40转换为DateTime。
我想将28/09/2009 17:30:40转换为DateTime。
我想将20090928 17:30:40转换为DateTime。



有多种可能的格式。我试过这个:

  string [] formats = new string [] {yyyymmdd,yyyymmddThhmmss,yyyy / mm / dd hh:mm:ss,yyyy / mm / dd,yyyy-mm-dd hh:mm:ss,yyyy-mm-dd}; 
IFormatProvider culture = new CultureInfo(en-US,true);
DateTime formattedDate = DateTime.ParseExact(aDate,formats,culture,DateTimeStyles.None);

此示例使用消息String未被识别为有效的DateTime引发异常。 p>

上面的代码有什么问题?

解决方案

第一天,如下所示:dd / MM / yyyy



还要注意资本M ,因为小写m是分钟。你的小时也有类似的问题;由于您的样品全部使用24小时,您需要一个资本H。



您的格式字符串数组应如下所示:

  string [ ] formats = {dd / MM / yyyy,yyyy-MM-dd HH:mm:ss,dd / MM / yyyy HH:mm:ss,yyyyMMdd HH:mm:ss}; 

这些格式与您提供的示例字符串完全相符。



此外,您可能希望在这种情况下使用不变文化,而不是 en-US 。否则,您的格式字符串中的/字符确实是一个文化特定的日期分隔符,用户可能会在本地系统上过度使用。



最后,您显然无法将字符串匹配,您可能需要使用 TryParseExact(),其工作方式与解析精确类似,但使用 out 参数,而不是返回值,以便它可以返回一个布尔值来指示成功或失败,而不是抛出异常。



查看完整格式字符串参考:

http://msdn.microsoft .com / en-us / library / 8kb3ddd4.aspx


How to convert the string "28/09/2009" to DateTime in a specific format? Ex: I want to convert "2009-09-28 17:30:40" to DateTime. I want to convert "28/09/2009 17:30:40" to DateTime. I want to convert "20090928 17:30:40" to DateTime.

There are multiple possible formats. I tried this:

string[] formats = new string[] {"yyyymmdd","yyyymmddThhmmss","yyyy/mm/dd  hh:mm:ss","yyyy/mm/dd","yyyy-mm-dd hh:mm:ss","yyyy-mm-dd"};
IFormatProvider culture = new CultureInfo("en-US", true);
DateTime formattedDate = DateTime.ParseExact(aDate, formats, culture, DateTimeStyles.None);

This example throws an exception with the message "String was not recognized as a valid DateTime".

What's wrong in the code above?

解决方案

None of your formats put the day first, like this: "dd/MM/yyyy".

Also note the capital 'M', since lower case 'm' is for 'minutes'. You have a similar problem with your hours; since your samples all use 24 hour time you need a capital 'H'.

Your format string array should look like this:

string[] formats = {"dd/MM/yyyy", "yyyy-MM-dd HH:mm:ss", "dd/MM/yyyy HH:mm:ss", "yyyyMMdd HH:mm:ss"};

Those formats exactly match your supplied sample strings.

Additionally, you probably want to use the invariant culture rather than en-US in this case. Otherwise, the '/' character in your format strings is really a culture-specific date separator, which a user might over-ride on their local system.

Finally, since you're obviously having trouble matching up the strings up, you might want to use TryParseExact(), which works just like parse exact but uses an out parameter rather than returning the value, so that it can return a boolean to indicate success or failure rather than throwing an exception.

See the complete format string reference here:
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

这篇关于如何将字符串转换为c#中的特定DateTime格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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