DateTime.TryParseExact对于MM/dd/yyyy hh:mm返回false

编程入门 行业动态 更新时间:2024-10-18 06:03:55
本文介绍了DateTime.TryParseExact对于MM/dd/yyyy hh:mm返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

以下代码为我返回了false,我只是弄不清楚我在做什么错.

Following code returns false for me and I just can't figure out what am I doing wrong.

var localDateTimeString = "03/24/2016 21:05"; // subject.Substring(0, 16); DateTime localDateTime; if (!DateTime.TryParseExact( localDateTimeString, "MM/dd/yyyy hh:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out localDateTime)) return false;

格式是固定的16个字符的字符串,日期,月份,分钟和小时始终为2位数字.年份4位数字.

Format is fixed 16 char string, always 2 digits for days, month, minutes and hour. 4 digits for year.

但是此代码返回false,我该如何解决?

But this code returns false, how can I fix this?

推荐答案

短版本应为HH而不是hh.除非将一位数字小时(0到9)作为一位数字返回;否则,否则为0.在这种情况下,您应该使用H.

Short version it should be HH not hh. Unless the single digit hours (0 to 9) are returned as a single digit; in that case you should use H.

来自 MSDN:

  • HH:小时,使用24小时制,从00到23.
  • hh:小时,使用从12到12的时钟(从01到12).
  • H:小时,使用0到23之间的24小时制.
  • h:小时,使用1到12之间的12小时制.
    • HH: The hour, using a 24-hour clock from 00 to 23.
    • hh: The hour, using a 12-hour clock from 01 to 12.
    • H: The hour, using a 24-hour clock from 0 to 23.
    • h: The hour, using a 12-hour clock from 1 to 12.
    • 基本上从以下位置更改格式:

      Essentially change your format from:

      "MM/dd/yyyy hh:mm"

      收件人:

      "MM/dd/yyyy HH:mm"

更多推荐

DateTime.TryParseExact对于MM/dd/yyyy hh:mm返回false

本文发布于:2023-11-07 19:41:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1567245.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:MM   dd   DateTime   TryParseExact   mm

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!