简体   繁体   中英

String was not recognized as valid DateTime c#

为什么这条线不起作用?

DateTime myDate = DateTime.ParseExact("04:05:14:17:17:09", "DD:MM:YY:HH:MM:SS", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.NoCurrentDateDefault);

This format is very much not what you're trying to do.

Try changing it to dd:MM:yy:HH:mm:ss :

DateTime myDate = DateTime.ParseExact("04:05:14:17:17:09", "dd:MM:yy:HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.NoCurrentDateDefault);

So, what have I changed:

  1. MM refers to months, mm is for minutes
  2. ss needs to be lowercase
  3. dd needs to be lowercase
  4. yy needs to be lowercase

And just for the record, I left HH uppercase because that means it's 24-hour, but lowercase would be 12 hour.

You might want to review the Custom Date and Time Format Strings MSDN page, where all these are explained. It has some good examples that might help 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