解析日期时间字符串 [英] Parse Datetime string

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

问题描述

我想下面的字符串转换为日期时间。我搜索高和低,不能找到确切的格式字符串,我不想诉诸手动分析它。

I'm trying to convert the following string to datetime. I've searched high and low and can't find the exact formats string and I don't want to resort to parsing it manually.

var dateString = "20110828T134108+0100";

我所有的尝试失败,并出现FormatException。

All my attempts fail with FormatException.

推荐答案

您是否尝试过这个?

var date = DateTime.ParseExact( dateString
   ,"yyyyMMdd\THHmmsszzz"
   ,CultureInfo.InvariantCulture 
   );  

MSDN

如果格式是自定义格式图案不包括日期或   时间分隔符(如年月日HHMM),使用固定区域性   为提供参数,每个自定义格式最广泛的形式   说明。例如,如果你想要的格式指定小时数   图案,指定,而不是狭义的形式更广泛的形式,HH,   H。

If format is a custom format pattern that does not include date or time separators (such as "yyyyMMdd HHmm"), use the invariant culture for the provider parameter and the widest form of each custom format specifier. For example, if you want to specify hours in the format pattern, specify the wider form, "HH", instead of the narrower form, "H".

这篇关于解析日期时间字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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