Is there an API to convert from “YYYYMMDDHHMMSS.UUUUUU-TZO” format to C# DateTime?
https://stackoverflow.com/questions/928051
Question
Example: "20080807144334.410187-180" (-180 means GMT minus three hours. Rio de Janeiro in this case.)
That string format is returned when I query file creation/change/access times via WMI (that is not totally working; see here). I guess I could parse it the idiot way, extracting year, month etc. from the string positions. But I'd like not to reinvent the wheel. System.DateTime's constructors don't handle that format. Should I go on and do it the idiot way or is there something better?
Solution
You should be able to use DateTime.ParseExact
or .TryParseExact
to give it the specific format to use when parsing.
However, I don't think you can get it to read your time zone in that format (though I can't actually figure out how to get it to read a time zone in any format).
The rest of it would look like this:
DateTime.ParseExact("20080807144334.410187", "yyyyMMddHHmmss.ffffff", System.Globalization.CultureInfo.InvariantCulture)
OTHER TIPS
You should take a look at the DateTime.TryParseExact method. It'll let you pass in your format that you're converting from.
http://msdn.microsoft.com/en-us/library/system.datetime.tryparseexact.aspx