Clearly, your string and format does not match.
From documentation;
Converts the specified string representation of a date and time to its
DateTime equivalent. The format of the string representation must
match a specified format exactly.
You need to use M/dd/yyyy
with a culture that has /
as a DateSeparator
like InvariantCulture
.
string _toDate = "5/22/2015";
DateTime myDate = DateTime.ParseExact(_toDate, "M/dd/yyyy", CultureInfo.InvariantCulture);
When you use null
as an IFormatProvider
, it's threaded as your CurrentCulture
and if your CurrentCulture
doesn't have /
as a DateSeparator
, you will get FormatException
because /
custom format specifier has a special meaning as replace me with current culture or supplied culture date separator.