简体   繁体   中英

"String was not recognized as a valid DateTime in Asp.net

string JDate = "21/01/2016 3:47 PM";

when I convert jDate to DateTime , An error Occurred

DateTime journeyDate = DateTime.ParseExact(JDate, "MM/dd/yyyy hh:mm:ss", CultureInfo.InvariantCulture);

String was not recognized as a valid DateTime.

您需要为PM添加TT

You should add tt form PM as @SLaks said to convert the string to DateTime

string JDate = "21/01/2016 3:47 PM"; 
DateTime journeyDate = DateTime.ParseExact(JDate, "dd/MM/yyyy h:mm tt", CultureInfo.InvariantCulture);

Adding tt will fix the issue.

"dd/MM/yyyy h:mm tt"

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