Uğurcan Erkal
Uğurcan Erkal

Reputation: 53

String was not recognized as a valid DateTime Exception

string datestring = txtNewReminderRemindDate.Text.ToString() + " " + RemTime.ToString();

So my datestring is "17/5/2017 19:10:00"

I'm trying to convert this string to put my Notification. But when I do this:

DateTime alarm = DateTime.ParseExact(datestring, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);

I get this:

Unhandled Exception:

System.FormatException: String was not recognized as a valid DateTime.

I don't understand what I'm doing wrong. I even tried:

DateTime alarm = DateTime.Parse(datestring);

Upvotes: 0

Views: 26507

Answers (1)

Hossein Narimani Rad
Hossein Narimani Rad

Reputation: 32521

Just replace MM part with M. The month (5) in your string (17/5/2017 19:10:00) is only one digit, not two digits. So you shouldn't use MM.

 DateTime.ParseExact(datestring, "dd/M/yyyy HH:mm:ss", CultureInfo.InvariantCulture);

Upvotes: 7

Related Questions