Monday, May 05, 2014

String was not recognized as a valid DateTime.- While parsing Date String with ParseExact

You may encounter the following error while parsing with DateTime.ParseExact

d = DateTime.ParseExact("02/05/1982", "dd/MM/yyyy", null)

Exception

String was not recognized as a valid DateTime

Solutions

This error can be solved with any one of the following solution

Solution 1

Pass the Current Invariant Culture as the Format Provider

d = DateTime.ParseExact("02/05/1982", "dd/MM/yyyy", CultureInfo.InvariantCulture)

Solution 2

Change the format with Escape Sequences

d = DateTime.ParseExact("02/05/1982", "dd\\/MM\\/yyyy", null)

No comments:

Post a Comment