Reputation: 5549
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);
Upvotes: 3
Views: 3543
Reputation: 1891
Use DD and YYYY to lower case like below.
DateTime.TryParseExact("16062001", "ddMMyyyy", CultureInfo.InvariantCulture,DateTimeStyles.None,out parsed);
Upvotes: 1
Reputation: 4919
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);
Upvotes: 3
Reputation: 125650
Pattern string is case sensitive. You should use lowercased dd
and yyyy
.
DateTime.TryParseExact("16062001", "ddMMyyyy", CultureInfo.InvariantCulture,DateTimeStyles.None, out parsed);
Upvotes: 3
Reputation: 9583
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);
Upvotes: 6