Reputation: 13945
This:
DateTime newTime = DateTime.ParseExact(sectionDate, "MM/dd/yyyy hh:mm tt", System.Globalization.CultureInfo.InvariantCulture);
Throws this:
"String was not recognized as a valid DateTime."
When sectionDate
looks like:
"4/3/2017 05:22 PM"
What am I doing wrong?
Upvotes: 1
Views: 6789
Reputation: 6164
You must do one of the following:
1) Change your format string to: "M/d/yyyy hh:mm tt"
OR
2) Change your input to: "04/03/2017 05:22 PM"
OR
3) Change your code to:
DateTime newTime = DateTime.Parse(sectionDate);
Upvotes: 1
Reputation: 3668
This code worked for me
DateTime newTime = DateTime.ParseExact(sectionDate, "M/d/yyyy hh:mm tt", System.Globalization.CultureInfo.InvariantCulture);
Either use above formatter or change your sectionDate
to "04/03/2017 05:22 PM";
Upvotes: 3