I'm trying to convert a string into a DateTime in C#, but I'm getting this error:
System.FormatException: 'String was not recognized as a valid DateTime.'
The error is in the next line:
DateTime endTime = DateTime.ParseExact(endDate, "MM/dd/yyyy hh:mm:ss tt", null);
My endDate
variable has the following info: "10/03/2017 06:52:48 AM"
What am I doing wrong?
When you use null
as an IFormatProvider
, all DateTime
parsing methods use CurrentCulture
settings of your computer.
There are a few possibilities that you get exception. For example, your CurrentCulture
might not have AM
as it's AMDesignator
property.
Instead of that, use a proper culture like InvariantCulture
.
DateTime endTime = DateTime.ParseExact("10/03/2017 06:52:48 AM",
"MM/dd/yyyy hh:mm:ss tt",
CultureInfo.InvariantCulture);
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.