MikeTWebb
MikeTWebb

Reputation: 9279

DateTime Conversion issue

I have a string value in the following format. 23-SEP-10 10.48.53.0000 AM

When I try a DateTime.Parse() or a Convert.ToDateTime() on this string I get the following error.

"String was not recognized as a valid DateTime."

What do I need to do in order to get this to work? Thanks

Upvotes: 0

Views: 150

Answers (2)

Mangesh
Mangesh

Reputation: 3997

If you know the format you want to convert to then you should use DateTime.ParseExact. otherwise DateTime.Parse compares the date with millions of formats which you don't want.

Upvotes: 0

Oded
Oded

Reputation: 499002

Assuming .NET, you should be using a custom DateTime format string, in conjunction with ParseExact or TryParseExact.

Example in C#:

var parsedDateTime = DateTime.ParseExact("23-SEP-10 10.48.53.0000 AM", 
                                         "dd-MMM-yy hh.mm.ss.FFFF tt", 
                                         CultureInfo.InvariantCulture);

Upvotes: 5

Related Questions