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

标签 vb.net date globalization literals

我设置了日期变量

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

无论系统区域设置如何,此功能是否始终在运行时有效?

最佳答案

根据the MSDN documentation :

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.

因此,您的问题的答案是"is",它将始终在运行时工作,并且“否”,您不需要更改计算机的区域设置。

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

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

另请注意,Date 只是 System.DateTime 的 VB.Net 别名,同样是为了从 VB6 向后熟悉。无论您使用哪个并不重要,它们的含义都是相同的。

关于vb.net - .NET 日期变量规范和区域设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17912806/

相关文章:

mysql - MySQL 数据库列的时间格式无法识别

.net - 翻译数据库字符串

c# - 在 C# 中获取具有 2 位数年份的特定于文化的日期

vb.net - 如何将vb类添加到类库中?

vb.net - 返回在 vb.net 中获取 datagridview 单元格值

Javascript:OnChange 未触发

c# - 为什么 DateTimeFormatInfo.GetAllDateTimePatterns 在不同的 .NET 版本中返回不同的结果?

vb.net - Alt 代码没有导航出用户控制

swift - 快速从相机滚动的图像中获取创建日期

c# - 使用 Linq 计算 2 个日期之间的对象列表