views:

461

answers:

2

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?

+8  A: 

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)
bdukes
Thanks! It works and it doesn't reinvent the wheel, but by Zeus it's ugly. Behold:retVal.CreationDate = DateTime.ParseExact(((string)queryObj["CreationDate"]).Substring(0,21), "yyyyMMddHHmmss.ffffff", System.Globalization.CultureInfo.InvariantCulture);
JCCyC
+5  A: 

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

Scott Ivey