Thomas Freudenberg

Confessions of a caffeine addict

DateTime.ParseExact

If you're using DateTime.ParseExact with a custom format string including slashes, don't forget to escape them.

After I've catched FormatExceptions several times, I've found this explanation:

'/' is the default date separator defined in DateTimeFormatInfo.DateSeparator.  Therefore you have to escape '/' with '\' if you want to use it:

System.DateTime.ParseExact("2004/05/31 16:19:43", @"yyyy\/MM\/dd HH:mm:ss", null);

Comments

Anonymous said:

Thanks a lot! this solved my datetime problem :-)
# August 6, 2004 11:53 AM

Anonymous said:

R U caught by M$ like a few others who don't blog anymore or what's going on Thoemmi?
# August 26, 2004 2:03 AM

Hato0be said:

For me, don't know why... but works fine and without escaping the slashes, the date was in other format but i think it's the same..

i used this command:

DateTime.ParseExact("2004/05/31", "dd/MM/yyyy", null);

# February 14, 2007 6:15 AM

Hato0be said:

..sorry, i mixed up the command, it was..

DateTime.ParseExact("14/02/2007", "dd/MM/yyyy", null);

# February 14, 2007 6:19 AM

Thomas Freudenberg said:

Hato0be, it depends on your regional settings. For example on my machine I set the date separator to '-', so the code above throws a FormatException if the string to be parsed contains slashes.

# February 14, 2007 6:30 AM

Rick said:

Thanks man!!!! solve my problem too.... 10q

# September 13, 2007 8:11 PM

Mitoman said:

I am trying to parse 200806251630 into date time, and i am kinda having problem with parsing it this way

# June 25, 2008 11:41 AM

NewP said:

I am not sure what I am doing wrong.

I am putting value in a datatable. Column type is DateTime.

nextRow[colIndex]= DateTime.ParseExact("2008-07-10, @"yyyy-MM-dd",null);

It always puts value in default format. So the resultant date that I see looks like "07/10/2008 12:00:00 AM"

I don't understand why.

any ideas?

Thanks

# July 10, 2008 9:10 AM

Thomas Freudenberg said:

NewP, ParseExact works as expected. I tried

DateTime dt = DateTime.ParseExact("2008-07-10", @"yyyy-MM-dd",null);

and dt has the correct values.

I assume that you convert the DateTime object to a string accidentally, which will the default format (like dt.ToString())

# July 10, 2008 10:07 AM

NewP said:

At my end dt = {7/10/2008 12:00:00 AM}

right after I execute

DateTime dt = DateTime.ParseExact("2008-07-10", @"yyyy-MM-dd",null);

in watch window. So ... what could it be?

I am pulling my hair on this ...

thanks ..I appreciate it.

# July 10, 2008 11:07 AM

Thomas Freudenberg said:

That's correct, because the watch window calls .ToString() to display objects.

# July 10, 2008 11:11 AM

Mac said:

I am parsing "2008-04-31T13:20:00.000"

Any idea what could be the format? i am using yyyy-MM-ddTHH:mm:ss but it does not work.

# August 14, 2008 1:42 AM

Thomas Freudenberg said:

Mac, a) April has only 30 days, and b) your format string does not specify the seconds fraction. Following line works fine:

DateTime parsedDate = DateTime.ParseExact(

   "2008-05-31T13:20:00:000",

   "yyyy-MM-ddTHH:mm:ss:fff", null);

# August 14, 2008 2:17 AM

garfield said:

Thank you..

# July 30, 2009 5:18 AM

Robert said:

Brilliant... Just what I was looking for to save me at the end of a long day...

# August 24, 2009 8:08 PM

Dani said:

thanks. this solved my problem

# September 8, 2009 4:59 AM

Sabastesy said:

nice

# September 16, 2009 4:08 PM

Chris said:

Hi ,

I have a similar problem in that I am trying to find the correct datetime.parseexact format for the us 12 hour format e.g 9/14/2009 12:00:00 AM

Do you know how i need to specify the format string to parse these?

Any help is greatly appreciated.

Thanks

Chris

# September 18, 2009 9:16 AM

reedone816 said:

@Chris,

just like the example, @"yyyy\/MM\/dd HH:mm", exclude the second

btw. in visual studio 2008, you don't need the escape anymore, so you just do it like this (in vb):

dim getdate as date

getdate=DateTime.ParseExact("2004/05/31", "yyyy/MM/dd", Nothing)

and you change the null to Nothing, otherwise it will generate error, and remember no space in "2004/05/31"

# October 5, 2009 7:18 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)