挪威文化的无效日期时间格式异常
Invalid datetime format exception for Norwegian culture
在我的 web.config 文件中有这一行:
<globalization culture="auto:en-GB" uiCulture="auto:en-GB" requestEncoding="utf-8" responseEncoding="utf-8" responseHeaderEncoding="utf-8"/>
ASP.Net 很好地处理了所有 date/time 格式。在示例页面中应用以下代码...
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
使用浏览器,设置为英语(英国)我看到日期显示为
25/01/2010 00:00:00
按下按钮并将其转换回 DateTime 值后,效果很好。
如果我将浏览器 (Chrome) 设置更改为 Norwegian Bokmal,并加载页面,我会看到:
25.01.2010 00.00.00
这又是正确的,但是,如果我随后提交表单,ASP.Net 崩溃:
String was not recognized as a valid DateTime.
Line 9: Dim dt As DateTime = Convert.ToDateTime(txt.Text.Trim())
为什么会这样?当然,如果 ASP.Net 能够根据文化设置 显示 日期,它应该能够读取它们吗?我试过英国英语和美国英语,两者都按预期工作,还有其他一些,所以它似乎以某种方式与挪威语有关。
使用 Convert.ToDateTime(string, IFormatProvider)
重载指定要使用的 CultureInfo
。
终于找到答案了。这是 v4.0+ 的 .Net 框架中的一个错误。
已经reported in Microsoft Connect, and a temporary workaround is detailed here。显然这是与 Windows 10:
严格相关的问题
Windows 10 changes the date and time formatting settings for some
cultures. Of particular concern are seven cultures for three different
regions:
- Finnish
- Norwegian Bokmål (“Norway” and “Svalbard and Jan Mayen” variants)
- 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. 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.
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. 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. Now,
DateTime.Parse(DateTime.Now.ToString()) no longer works under the
described conditions. 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.
官方补丁将于 2015 年 9 月发布。
在我的 web.config 文件中有这一行:
<globalization culture="auto:en-GB" uiCulture="auto:en-GB" requestEncoding="utf-8" responseEncoding="utf-8" responseHeaderEncoding="utf-8"/>
ASP.Net 很好地处理了所有 date/time 格式。在示例页面中应用以下代码...
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
使用浏览器,设置为英语(英国)我看到日期显示为
25/01/2010 00:00:00
按下按钮并将其转换回 DateTime 值后,效果很好。
如果我将浏览器 (Chrome) 设置更改为 Norwegian Bokmal,并加载页面,我会看到:
25.01.2010 00.00.00
这又是正确的,但是,如果我随后提交表单,ASP.Net 崩溃:
String was not recognized as a valid DateTime.
Line 9: Dim dt As DateTime = Convert.ToDateTime(txt.Text.Trim())
为什么会这样?当然,如果 ASP.Net 能够根据文化设置 显示 日期,它应该能够读取它们吗?我试过英国英语和美国英语,两者都按预期工作,还有其他一些,所以它似乎以某种方式与挪威语有关。
使用 Convert.ToDateTime(string, IFormatProvider)
重载指定要使用的 CultureInfo
。
终于找到答案了。这是 v4.0+ 的 .Net 框架中的一个错误。
已经reported in Microsoft Connect, and a temporary workaround is detailed here。显然这是与 Windows 10:
严格相关的问题Windows 10 changes the date and time formatting settings for some cultures. Of particular concern are seven cultures for three different regions:
- Finnish
- Norwegian Bokmål (“Norway” and “Svalbard and Jan Mayen” variants)
- 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. 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.
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. 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. Now, DateTime.Parse(DateTime.Now.ToString()) no longer works under the described conditions. 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.
官方补丁将于 2015 年 9 月发布。