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

查看:114
本文介绍了.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));
}

我得到了以下内容:

I get the following:

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

和我的问题......为什么会失败在123和12345?如果不是那些已经成为1点23分零零秒和1时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?

更新:那么,好像大家可能都想通了,为什么这个失败之类的。好像 ^ h 实际上是抓住两个数字,然后只留下一个用于毫米,那么这将失败。但是,有没有人对如何我可以改变这个code,这样我会得到我要寻找的结果是个好主意?

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?

另一个更新:想我已经找到一个合理的解决方案了。增加一条,作为一个答案。将接受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 M 取值的实际把持两位数字的时候就可以了,即使不会有足够的位数,其余的格式。因此,例如与格式的的和数字的 123 ^ h 的会抓住的 12 的和只会有一个的 3 的离开了。而毫米的需要两位数字,因此它失败。 Tadaa 的。

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",
    };

随着从我的问题code保持相同的休息,然后我会得到这个结果:

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天全站免登陆