简体   繁体   中英

DateTime.ParseExact() - String was not recognized as a valid DateTime

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?

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";

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM