3

I am trying to parse a string date which is in DDMMYYYY format using following code but it is returning false even though 16062001 is a valid date in DDMMYYYY format.

DateTime.TryParseExact("16062001", "DDMMYYYY", CultureInfo.InvariantCulture,DateTimeStyles.None,out parsed);
SamuraiJack
  • 5,131
  • 15
  • 89
  • 195

4 Answers4

6

Try with lower case d and y as per https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx

Eg.

DateTime.TryParseExact("16062001", "ddMMyyyy", CultureInfo.InvariantCulture,DateTimeStyles.None,out parsed);
Steve
  • 9,335
  • 10
  • 49
  • 81
3

Pattern string is case sensitive. You should use lowercased dd and yyyy.

DateTime.TryParseExact("16062001", "ddMMyyyy", CultureInfo.InvariantCulture,DateTimeStyles.None, out parsed);
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
3

According to this document: http://www.csharp-examples.net/string-format-datetime/

Your format should be this one instead: "ddMMyyyy"

Try changing to this one:

DateTime.TryParseExact("16062001", "ddMMyyyy", CultureInfo.InvariantCulture,DateTimeStyles.None,out parsed);
User2012384
  • 4,769
  • 16
  • 70
  • 106
1

Use DD and YYYY to lower case like below.

DateTime.TryParseExact("16062001", "ddMMyyyy", CultureInfo.InvariantCulture,DateTimeStyles.None,out parsed);
Kaushik Maheta
  • 1,741
  • 1
  • 18
  • 27
  • 2
    Though a fine answer, it is essentially the same as the other **3**, all answered 6 minutes earlier. –  Nov 20 '15 at 04:55