.NET:为什么TryParseExact在Hmm和Hmmss上失败? [英] .NET: Why is TryParseExact failing on Hmm and Hmmss?

查看:137
本文介绍了.NET:为什么TryParseExact在Hmm和Hmmss上失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 DateTime.TryParseExact 方法,我遇到了一个我不知道的情况。我有一些格式和一些主题来解析,每个都应该完美匹配一种格式:

I'm trying out the DateTime.TryParseExact method, and I have come over a case that I just don't get. I have some formats and some subjects to parse that each should match one of the formats perfectly:

var formats = new[]
     {
         "%H",
         "HH",
         "Hmm",
         "HHmm",
         "Hmmss",
         "HHmmss",
     };

var subjects = new[]
     {
         "1",
         "12",
         "123",
         "1234",
         "12345",
         "123456",
     };

然后我尝试解析它们,并打印出结果:

I then try to parse them all and print out the results:

foreach(var subject in subjects)
{
    DateTime result;
    DateTime.TryParseExact(subject, formats, 
        CultureInfo.InvariantCulture, 
        DateTimeStyles.NoCurrentDateDefault,
        out result);

    Console.WriteLine("{0,-6} : {1}", 
        subject,
        result.ToString("T", CultureInfo.InvariantCulture));
}

我得到以下内容:

1      : 01:00:00
12     : 12:00:00
123    : 00:00:00
1234   : 12:34:00
12345  : 00:00:00
123456 : 12:34:56


$ b $对我的问题,为什么123和12345失败?不应该是01:23:00和01:23:45吗?我在这里缺少什么?而且我该如何看待它呢?

And to my question... why is it failing on 123 and 12345? Shouldn't those have become 01:23:00 and 01:23:45? What am I missing here? And how can I get it to work as I would expect it to?

更新: / strong>所以,似乎我们可能已经弄清楚为什么这是失败的。看起来像 H 实际上是抓住两位数字,然后只留下一个 mm ,然后会失败。但是,有没有人有一个好主意,我可以如何更改这个代码,以便获得我正在寻找的结果?

Update: So, seems like we might have figured out why this is failing sort of. Seems like the H is actually grabbing two digits and then leaving just one for the mm, which would then fail. But, does anyone have a good idea on how I can change this code so that I would get the result I am looking for?

em>另一个更新:想想我现在找到了一个合理的解决方案。添加它作为答案。将会在2天内接受,除非有人想出更好的一个。感谢您的帮助!

Another update: Think I've found a reasonable solution now. Added it as an answer. Will accept it in 2 days unless someone else come up with an even better one. Thanks for the help!

推荐答案

好的,所以我想我已经把这全部了,实验和其他有用的答案。发生了什么事情呢,即使没有足够的数字可以使用 H 的格式。所以例如格式 Hmm 和数字 123 H 将抓住 12 ,只有一个 3 左。而 mm 需要两位数字,因此失败。

Ok, so I think I have figured this all out now thanks to more reading, experimenting and the other helpful answers here. What's happening is that H, m and s actually grabs two digits when they can, even if there won't be enough digits for the rest of the format. So the for example with the format Hmm and the digits 123, H would grab 12 and there would only be a 3 left. And mm requires two digits, so it fails. Tadaa.

所以,我的解决方案目前只是使用以下三种格式:

So, my solution is currently to instead use just the following three formats:

var formats = new[]
    {
        "%H",
        "Hm",
        "Hms",
    };

与我的问题的其余代码保持一致,我会得到这个结果:

With the rest of the code from my question staying the same, I will then get this as a result:

1      : 01:00:00
12     : 12:00:00
123    : 12:03:00
1234   : 12:34:00
12345  : 12:34:05
123456 : 12:34:56

我认为应该是合理和可以接受的:)

Which I think should be both reasonable and acceptable :)

这篇关于.NET:为什么TryParseExact在Hmm和Hmmss上失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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