vb.net .NET 日期变量规范和区域设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17912806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:26:15  来源:igfitidea点击:

.NET Date variable specification & locale

vb.netdateglobalizationliterals

提问by Nuts

I set the date variable

我设置了日期变量

Dim myDate as Date
myDate = #5/15/2013#

Does this work always at runtime, no matter what is the system locale settings?

无论系统区域设置是什么,这是否总是在运行时工作?

回答by Matt Johnson-Pint

According to the MSDN documentation:

根据MSDN 文档

You must enclose a Date literal within number signs (# #). You must specify the date value in the format M/d/yyyy, for example #5/31/1993#. This requirement is independent of your locale and your computer's date and time format settings.

The reason for this restriction is that the meaning of your code should never change depending on the locale in which your application is running. Suppose you hard-code a Date literal of #3/4/1998# and intend it to mean March 4, 1998. In a locale that uses mm/dd/yyyy, 3/4/1998 compiles as you intend. But suppose you deploy your application in many countries. In a locale that uses dd/mm/yyyy, your hard-coded literal would compile to April 3, 1998. In a locale that uses yyyy/mm/dd, the literal would be invalid (April 1998, 0003) and cause a compiler error.

您必须将日期文字括在数字符号 (##) 内。您必须以 M/d/yyyy 格式指定日期值,例如 #5/31/1993#。此要求与您的区域设置和计算机的日期和时间格式设置无关。

这个限制的原因是你的代码的含义永远不应该根据你的应用程序运行的区域设置而改变。假设您对#3/4/1998# 的日期字面量进行硬编码,并打算将其表示为 1998 年 3 月 4 日。在使用 mm/dd/yyyy 的语言环境中,3/4/1998 按您的意图进行编译。但是假设您在许多国家/地区部署了应用程序。在使用 dd/mm/yyyy 的语言环境中,您的硬编码文字将编译为 1998 年 4 月 3 日。在使用 yyyy/mm/dd 的语言环境中,文字将无效(1998 年 4 月,0003)并导致编译器错误。

So, the answer to your question is YES, it will always work at runtime, and NO, you do not need to change this for the locale settings of the computer.

因此,您的问题的答案是肯定的,它始终会在运行时工作,并且不,您不需要为计算机的区域设置更改此设置。

Do keep in mind that date literals are somewhat frowned upon. They are supported in VB.Net as a backwards-familiarity thing from VB6. They don't even exist in other .Net languages like C#. If you have to hard-code a specific date, you are much better off using DateTimewith separate parameters, such as:

请记住,日期文字有些不受欢迎。它们在 VB.Net 中作为 VB6 向后熟悉的东西得到支持。它们甚至不存在于其他 .Net 语言(如 C#)中。如果您必须对特定日期进行硬编码,最好使用DateTime单独的参数,例如:

Dim myDate as DateTime
myDate = new DateTime(2013,5,15)

Also note that Dateis just a VB.Net alias to System.DateTime, again there for backwards familiarity from VB6. It doesn't matter which you use, they mean the same thing.

另请注意,这Date只是 VB.Net 的别名System.DateTime,再次从 VB6 向后熟悉。不管你使用哪个,它们的意思都是一样的。

回答by Robert Beaubien

No, that does not work in a country such as Canada where the standard is dd/MM/yyyy. The best way to globalize your application would be to use DateTime.ParseExact and/or DateTime.TryParseExact

不,这在加拿大等标准为 dd/MM/yyyy 的国家/地区不起作用。全球化应用程序的最佳方法是使用 DateTime.ParseExact 和/或 DateTime.TryParseExact

        Dim tempdate As DateTime

        tempdate = DateTime.ParseExact("05/20/2013", "MM/dd/yyyy", Globalization.CultureInfo.InvariantCulture)