tags:

views:

834

answers:

1

I have a datetime coming back from an XML file in the format:-

20080916 11:02

as in yyyymm hh:ss

How can i get the datetime.parse function to pick up on this? Ie parse it without erroring? (.net 2.0) Cheers

+17  A: 
DateTime.ParseExact(input,"yyyyMMdd HH:mm",null);

assuming you meant to say that minutes followed the hours, not seconds - your example is a little confusing.

The ParseExact documentation details other overloads, in case you want to have the parse automatically convert to Universal Time or something like that.

As @Joel Coehoorn mentions, there's also the option of using TryParseExact, which will return a Boolean value indicating success or failure of the operation - I'm still on .Net 1.1, so I often forget this one.

If you need to parse other formats, you can check out the Standard DateTime Format Strings.

Blair Conrad
WOW, I wish I knew about that!
Timothy Khouri
Fantastic.Thanks very much for that :)
anonym0use
I'm happy to be of service.
Blair Conrad
Don't forget DateTime.TryParseExact()
Joel Coehoorn
Also: standard Xml has it's own very specific DateTime format, and the .Net Xml tools should be able to read it.
Joel Coehoorn