简体   繁体   English

解析日期时间字符串

[英]Parse Datetime string

I'm trying to convert the following string to datetime. 我正在尝试将以下字符串转换为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";

All my attempts fail with FormatException. 我的所有尝试都失败了FormatException。

Have you tried this? 你试过这个吗?

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

From MSDN : 来自MSDN

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. 如果format是不包含日期或时间分隔符的自定义格式模式(例如“yyyyMMdd HHmm”),请使用provider参数的不变区域性和每个自定义格式说明符的最宽格式。 For example, if you want to specify hours in the format pattern, specify the wider form, "HH", instead of the narrower form, "H". 例如,如果要在格式模式中指定小时数,请指定更宽的形式“HH”,而不是更窄的形式“H”。

The documentation on the format string is here: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx 有关格式字符串的文档,请访问: http//msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

That should get you started. 这应该让你开始。 :) :)

试试这种格式: "yyyyMMdd'T'HHmmss"

你试过这个:

DateTime.ParseExact("20110828T134108+0100", "yyyyMMdd'T'HHmmsszzzz", CultureInfo.InvariantCulture);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM