简体   繁体   English

挪威文化的无效日期时间格式异常

[英]Invalid datetime format exception for Norwegian culture

In my web.config file I have this line: 在我的web.config文件中,有以下行:

<globalization culture="auto:en-GB" uiCulture="auto:en-GB" requestEncoding="utf-8" responseEncoding="utf-8" responseHeaderEncoding="utf-8"/>

ASP.Net nicely takes care of all date/time formats. ASP.Net很好地处理了所有日期/时间格式。 Applying the following code in a sample page... 在示例页面中应用以下代码...

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    txt.Text = New DateTime(2010, 1, 25).ToString()
End Sub

Protected Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click
    Dim dt As DateTime = Convert.ToDateTime(txt.Text.Trim())
    Trace.Warn(dt.ToString())
End Sub

With a browser, set to English (UK) I see the date displayed as 使用浏览器,将其设置为英语(英国),我看到的日期显示为

25/01/2010 00:00:00

On pressing the button and converting this back to a DateTime value, it works perfectly. 按下按钮并将其转换回DateTime值时,它可以完美工作。

If I change my browser (Chrome) settings to Norwegian Bokmal, and load the page, I see this: 如果我将浏览器(Chrome)设置更改为Norwegian Bokmal,并加载页面,则会看到以下内容:

25.01.2010 00.00.00

This again is correct, but , if I then submit the form, ASP.Net crashes: 这再次是正确的, 但是 ,如果随后我提交表单,ASP.Net将崩溃:

String was not recognized as a valid DateTime. 无法将字符串识别为有效的DateTime。

Line 9: Dim dt As DateTime = Convert.ToDateTime(txt.Text.Trim()) 第9行:Dim dt As DateTime = Convert.ToDateTime(txt.Text.Trim())

Why is this happening? 为什么会这样呢? Surely if ASP.Net has the sense to display dates based on culture settings, it should be able to read them? 当然,如果ASP.Net能够根据区域性设置显示日期,它应该能够读取它们吗? I've tried English UK and English US and both work as expected, plus others, so it seems linked to Norwegian in some manner. 我已经尝试过英国英语和美国英语,并且都可以按预期工作,以及其他方面都可以正常工作,因此它似乎与挪威语有某种联系。

Finally found the answer. 终于找到答案了。 It is a bug in the .Net framework for v4.0+. 这是.Net框架v4.0 +中的错误。

It has been reported in Microsoft Connect , and a temporary workaround is detailed here . Microsoft Connect报道, 这里有一个临时解决方法。 Apparently this is problem strictly related to Windows 10: 显然,这是与Windows 10严格相关的问题:

Windows 10 changes the date and time formatting settings for some cultures. Windows 10更改某些区域性的日期和时间格式设置。 Of particular concern are seven cultures for three different regions: 特别令人关注的是三个不同地区的七种文化:

  1. Finnish 芬兰
  2. Norwegian Bokmål (“Norway” and “Svalbard and Jan Mayen” variants) 挪威语Bokmål(“挪威”和“斯瓦尔巴特群岛和扬马延”的变体)
  3. Serbian (variants “Cyrillic, Kosovo”, “Latin, Montenegro”, “Latin, Serbia” and “Latin, Kosovo”). 塞尔维亚语(变体“西里尔语,科索沃”,“拉丁语,黑山”,“拉丁语,塞尔维亚”和“拉丁语,科索沃”)。

For these seven cultures, Windows 10 changes the date and time separators to be the same. 对于这七个区域性,Windows 10将日期和时间分隔符更改为相同。 For example, in Finnish, the standard date format used to be 26.8.2015 21:08, while it is now 26.8.2015 21.08 – note the subtle change in the time separator. 例如,在芬兰语中,标准日期格式以前是26.8.2015 21:08,而现在是26.8.2015 21.08 –请注意时间分隔符中的细微变化。

In all currently released versions of .NET, the DateTime.Parse method has a shortcoming: It always fails to parse a date or a date+time combination in a culture where the date/time separators are the same character. 在所有当前发布的.NET版本中,DateTime.Parse方法有一个缺点:在日期/时间分隔符为相同字符的区域性中,它始终无法解析日期或日期+时间组合。 This bug, together with Windows 10's culture changes, breaks the previously hard rule of DateTime.Parse always being able to parse the culture's default DateTime representation. 此错误与Windows 10的区域性更改一起打破了以前的DateTime硬规则.Parse始终能够解析区域性的默认DateTime表示形式。 Now, DateTime.Parse(DateTime.Now.ToString()) no longer works under the described conditions. 现在,DateTime.Parse(DateTime.Now.ToString())在上述条件下不再起作用。 Neither does DateTime.Parse(DateTime.Now.ToShortDateString()), which is somewhat counterintuitive since the changed time separator isn't even involved, but true nonetheless – the parser thinks it's parsing a time instead of a date. DateTime.Parse(DateTime.Now.ToShortDateString())也没有,这有点违反直觉,因为更改的时间分隔符甚至都没有涉及,但是仍然是正确的–解析器认为它是在解析时间而不是日期。

An official patch will be released in September 2015. 官方补丁将于2015年9月发布。

使用Convert.ToDateTime(string, IFormatProvider)重载来指定要使用的CultureInfo

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

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