Search code examples
c#asp.net.netinvariantculture

C# Parsing Date in "DDMMYYYY" format?


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

Solution

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