DateTime.TryParseExact对于MM/dd/yyyy hh:mm返回false [英] DateTime.TryParseExact returns false for MM/dd/yyyy hh:mm

查看:122
本文介绍了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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆