Reputation: 641
I have a date string 4/30/2016
I tried to convert it by using following code:
DateTime dt = DateTime.Parse(date);
and I am getting the error
The string was not recognized as a valid DateTime. There is an unknown word starting at index 0.
Can you please help me How can I solve this issue? and why this error is happening?
Upvotes: 2
Views: 2380
Reputation: 15217
There is a DateTime.ParseExact
method for such a tasks. You can provide it with expected date format.
CultureInfo provider = CultureInfo.InvariantCulture;
var format = "M/dd/yyyy";
var result = DateTime.ParseExact(dateString, format, provider)
Upvotes: 9