1

I my application I need to convert a string in the datetime format as below:

var k=    DateTime.Parse("20150129163809");

But its throwing up an error message that "String was not recognized as a valid DateTime."

But, when I do

DateTime.Parse("2015-01-29 16:38:09")

its working fine...

What could be wrong?

sony
  • 1,453
  • 3
  • 35
  • 79

1 Answers1

9

Use ParseExact to parse well-formatted strings:

var k = DateTime.ParseExact("20150129163809","yyyyMMddHHmmss",CultureInfo.InvariantCulture);

DateTime.Parse is fairly limited in its capabilities and does not try and "guess" what format the string is in.

D Stanley
  • 149,601
  • 11
  • 178
  • 240