简体   繁体   English

字符串未被识别为有效的DateTime c#

[英]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 : 尝试将其更改为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 MM表示月份, mm表示分钟
  2. ss needs to be lowercase ss需要小写
  3. dd needs to be lowercase dd必须为小写
  4. yy needs to be lowercase yy必须为小写

And just for the record, I left HH uppercase because that means it's 24-hour, but lowercase would be 12 hour. 仅作记录,我将HH大写,因为这意味着它是24小时,但小写是12小时。

You might want to review the Custom Date and Time Format Strings MSDN page, where all these are explained. 您可能需要查看“ 自定义日期和时间格式字符串 MSDN”页面,在此解释了所有这些内容。 It has some good examples that might help you. 它有一些很好的例子,可能会对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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