开发者

Format of a date used for initialisation

In a VBScript, I need to initialise a variable with a date. In my country we usually specify a date with this format: day month year

Here is what I'm trying to do in my VBScript:

Date = #07-06-1973#
MsgBox FormatDateTime(Date, vbLongDate)

but the date I get is July the 6th, where I was expecting June the 7th!

Now, as it seems the first n开发者_C百科umber represents the month, here is what I tried to do:

Date = #13-06-1973#
MsgBox FormatDateTime(Date, vbLongDate)

but now, the first number represents the day! (I get the following date: June the 13th)

I haven't found a way to force the date format, so that there is no more ambiguity.


Strictly speaking, VBScript supports date literals in the US and ISO formats only:

' US format:
Date = #10/19/2009#

' ISO format:
Date = #2009-10-19#

However, the date separators can be different (slash, dash, space etc), and the VBScript engine also recognizes dates containing abbreviated month names (#Oct 19, 2009#) and non-ambiguous dates with swapped date parts (#2009 19 10#). The latter explains why #13-06-1973# in your second example is recognized as June, 13th: the number 13 falls outside the acceptable range of months (1-12) so it's interpreted as the day part.

If you want to specify dates in your regional format (that is, the format specified in your Regional and Language Options), you can use the CDate function to convert date strings, for example:

' Russian format
Date = CDate("19.10.2009")

But using US and ISO date literals are more reliable and preferred.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜