简体   繁体   English

C#DateTime.TryParse格式相同,日期不同

[英]C# DateTime.TryParse same format, different date

DateTime res;
String s;

s = "Wed, 28 Mar 2012 10:30:52 GMT";
DateTime.TryParse(s, out res);
Console.WriteLine(s + " => " + res);

s = "Fri, 15 May 2009 20:10:57 GMT";
DateTime.TryParse(s, out res);
Console.WriteLine(s + " => " + res);

Output: 输出:

Wed, 28 Mar 2012 10:30:52 GMT => 01/01/0001 00.00.00
Fri, 15 May 2009 20:10:57 GMT => 15/05/2009 22.10.57

Why does it work only for some dates? 为什么它仅在某些日期有效? Obviously I ran that code on the same machine at the same time. 显然,我同时在同一台计算机上运行了该代码。

I expect that the long-date format of the culture you're working in probably only accepts the full month name as suggested by @VimalStan (have you confirmed this either way, yet?). 我希望您正在使用的文化的长期格式可能只接受@VimalStan建议的完整月份名称(您是否已经确认了这两种方法?)。

It should (IMHO) accept what you're trying to doing too, but I'm aware that various cultures can have "quirks" like this. 它(IMHO)也应该接受您正在尝试做的事情,但是我知道各种文化都可能有这样的“怪癖”。 eg perhaps "mar" is ambiguous in some cultures? 例如,“ mar”在某些文化中可能是模棱两可的? (and while it may not be ambiguous in your culture... perhaps some code has hung-over from one culture to another... I don't know how culture rules are even implemented, so don't really know if this is even a valid suggestion... but my point that a particular culture may not always behave as expected, I think, is fair). (虽然它在您的文化中可能不是模棱两可的……也许某些代码已经从一种文化中转移到另一种文化中了……我不知道文化规则是如何实施的,所以真的不知道这是否是甚至是有效的建议……但我认为特定文化可能并不总是表现出预期,这是公平的)。

Use http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.currentculture.aspx to check which culture your .NET context is running in. You can view what context your windows user profile is running in via the control panel (eg "Region and Language" on Win 7), and take a peek at the long date format there. 使用http://msdn.microsoft.com/zh-cn/library/system.globalization.cultureinfo.currentculture.aspx来检查您的.NET上下文正在运行的区域性。您可以查看Windows用户配置文件在哪个上下文中运行通过控制面板(例如Win 7上的“区域和语言”),并查看那里的长日期格式。

Your example works fine on my pc, where the long date format is "dddd, d MMMM yyyy". 您的示例在我的PC上工作正常,长日期格式为“ dddd,d MMMM yyyy”。 I'm in Australia, using en-au. 我在澳大利亚,使用en-au。

As a test, try using en-au "English (Australia) as your culture (via control panel, or explicitly set as per the above currentculture link, at the "Explicitly Setting the CurrentCulture Property" heading, and then test if your code works as expected. 作为测试,请尝试使用en-au“英语(澳大利亚)”作为您的文化(通过控制面板,或根据上述currentculture链接进行显式设置,位于“显式设置CurrentCulture属性”标题下),然后测试您的代码是否有效如预期的那样。

If it works as expected, I think that means the problem is just that your culture is a little more strict in its parsing, than eg mine is (and eg than what you expected). 如果它能按预期工作,那么我认为这意味着问题是您的文化在解析方面比我的文化(例如,比您的预期要严格)更为严格。 And hence, you may need to ensure you pass the full month, or specify your own specific parsing pattern as per another answer here using TryParseExact() . 因此,您可能需要确保通过整个月,或者使用TryParseExact()根据此处的另一个答案指定自己的特定解析模式。

I had the same sort of issue the other day, it was very frustrating as there was no need for it to fail. 前几天,我遇到了同样的问题,这很令人沮丧,因为它没有必要失败。

However the way i fixed the issue was to move to DateTime.TryParseExact 但是我解决问题的方法是移动到DateTime.TryParseExact

which is used like: 用法如下:

// Parse date with no style flags.
dateString = " 5/01/2009 8:30 AM";
if (DateTime.TryParseExact(dateString, "g", enUS, 
                          DateTimeStyles.None, out dateValue))
   Console.WriteLine("Converted '{0}' to {1} ({2}).", 
                             dateString, 
                              dateValue, 
                             dateValue.Kind);
else
   Console.WriteLine("'{0}' is not in an acceptable format.", dateString);

Since switching parsing has never failed 由于切换解析从未失败

Try this: 尝试这个:

DateTime res;
String s = "Wed, 28 Mar 2012 10:30:52 GMT";
DateTime.TryParseExact(s, "R", CultureInfo.InvariantCulture, DateTimeStyles.None, out res);

This will give: {3/28/2012 10:30:52 AM} 这样可以{3/28/2012 10:30:52 AM}{3/28/2012 10:30:52 AM}

Also, when 而且,当

String s = "Fri, 15 May 2009 20:10:57 GMT";

This will give: {5/15/2009 8:10:57 PM} 这将使: {5/15/2009 8:10:57 PM}

It's only a guess, but perhaps the matter is that in italian "Mar" is the beginning of the word "Martedì" (Tuesday), so "Wed, 28 Mar 2012 10:30:52 GMT" could be misinterpreted as something like "Wednesday Martedì/Tuesday" which is absurd. 只是一个猜测,但是问题可能是意大利语中的“ Mar”是“Martedì”(星期二)一词的开头,因此“ Wed,2012 Mar 28,10:30:52 GMT”可能会被误解为类似“荒谬的“星期三Martedì/ Tuesday”。

As a counterexample, I looked for a Martedì/Tuesday 28 in 2013 and it works substituting the day of week with the month: 作为反例,我在2013年寻找Martedì/ Tuesday 28,它可以代替星期几和月份:

May, 28 Mar 2013 10:30:52 GMT => 28/05/2013 12.30.52 2013年5月28日10:30:52 GMT => 2013年5月28日12.30.52

Obviously it couldn't work using other locales. 显然,使用其他语言环境无法正常工作。

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

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