gpt4 book ai didi

vb.net - .NET 日期变量规范和语言环境

转载 作者:行者123 更新时间:2023-12-02 04:56:27 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.

所以,你的问题的答案是肯定的,它总是在运行时工作,否,你不需要为计算机的区域设置更改它。

请记住,日期文字有些令人不悦。 VB.Net 支持它们作为 VB6 的向后熟悉的东西。它们甚至不存在于其他 .Net 语言(如 C#)中。如果您必须对特定日期进行硬编码,最好使用带有单独参数的 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