Convert string to datetime

S

simonZ

I have string 20070502144551 and I would like to convert this to datetime:

2007-05-02 14:45:51

Is there some function?

Regards,Simon
 
P

Peter Duniho

I have string 20070502144551 and I would like to convert this to
datetime:

2007-05-02 14:45:51

DateTime.ParseExact("20070502144551", "yyyyMMddhhmmss", null);

Or, something like that anyway.
 
S

simonZ

If I try this, I get an error:
String was not recognized as a valid DateTime.

Any idea?

Thanks,Simon
 
J

Jimmy V

If I try this, I get an error:
String was not recognized as a valid DateTime.

Any idea?

Thanks,Simon








- Show quoted text -

This worked for me

DateTime.ParseExact("20070502024551", "yyyyMMddhhmmss", null);

I changed the 14 to 02, you may need a format provider to convert to
24 hour time.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

simonZ said:
If I try this, I get an error:
String was not recognized as a valid DateTime.

Any idea?

Thanks,Simon

Use HH instead of hh, and use CultureInfo.InvariantCulture instead of null.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top