简体   繁体   中英

Parse Datetime string

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";

All my attempts fail with FormatException.

Have you tried this?

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

From 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. For example, if you want to specify hours in the format pattern, specify the wider form, "HH", instead of the narrower form, "H".

The documentation on the format string is here: 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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