简体   繁体   中英

String was not recognized as a valid DateTime

DateTime dtEndTime = DateTime.ParseExact(
    appToDate, 
    timeFormats, 
    null, 
    System.Globalization.DateTimeStyles.None);

appToDate = 21-02-2013 12:30 AM

string[] timeFormats = { 
    "dd-MM-yyyy H:m tt", 
    "dd-MM-yyyy H:mm tt", 
    "dd-MM-yyyy HH:m tt", 
    "dd-MM-yyyy HH:mm tt" 
};

String was not recognized as a valid DateTime.

I suspect the problem is your use of H combined with tt . H and HH indicate an hour in the range 0-23, where 12 is noon, and therefore PM.

I suspect you want h and hh instead of H ... although you shouldn't need every combination of h / H / m / mm . (Do you really expect to see "1:5 PM"?) I suspect just "dd-MM-yyyy H:mm tt" should cover you.

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