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

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

Anonymous

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.

Anonymous

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.

Thomas Freudenberg

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())

Anonymous

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

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

Thomas Freudenberg

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);

Anonymous

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

reedone816

@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”

Mitoman

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

Daniel Fisher(lennybacon)

R U caught by M$ like a few others who don’t blog anymore or what’s going on Thoemmi?

Anonymous

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);

Thoemmi

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.

Leave a Comment

Your email address will not be published. Required fields are marked *

Loading...