Vijeeshkumar vofox
Vijeeshkumar vofox

Reputation: 641

String to datetime conversion "M/dd/yyyy"

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

Answers (1)

Sly
Sly

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

Related Questions