1

i have a strange behaviour when using the DateTime.ParseExact()

Works fine

DateTime.ParseExact("130314111405.000", "ddMMyyhhmmss.fff", null);

Format Exception

DateTime.ParseExact("130314130001.000", "ddMMyyhhmmss.fff", null);

Thanks for any helpful thoughts.

Juan
  • 4,910
  • 3
  • 37
  • 46

3 Answers3

4

Your hour part is 24 hours format. You need HH not hh which is for 12 hours format.

DateTime.ParseExact("130314130001.000", "ddMMyyHHmmss.fff", null); 
                                             //^^^Here

Hour 13 can't be parsed with hh which supports hours from 0-12.

I am not sure why you are passing null for IFormatProvider, you should (see this answer) use CultureInfo.InvariantCulture like:

DateTime.ParseExact("130314130001.000", "ddMMyyHHmmss.fff", CultureInfo.InvariantCulture); 
Community
  • 1
  • 1
Habib
  • 219,104
  • 29
  • 407
  • 436
2

Problem : You are trying to parse the Hours value which is 13 using small hh , which is not supported by small hh because From MSDN: hh Custom Format

The hour, using a 12-hour clock from 01 to 12.

So You need to use HH instead of hh from MSDN HH Custom Format

The hour, using a 24-hour clock from 00 to 23.

Try This:

 DateTime.ParseExact("130314130001.000", "ddMMyyHHmmss.fff", null);
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
1

Use DateTime.ParseExact("130314130001.000", "ddMMyyHHmmss.fff", null);

hh is only for 12-hour time, where HH is for 24-hour

Siyual
  • 16,415
  • 8
  • 44
  • 58