gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 21:55:19 25 4
gpt4 key购买 nike

我设置了日期变量

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com