Forcing Date/Time

There are different ways that we can force the subtype to be Date instead of String:

Dim varTest
varTest = #5/16/99 12:30 PM#
MsgBox TypeName(varTest)

This example surrounds the date/time value in # signs instead of quotes. This is the VBScript way of identifying a date literal (VB/VBA uses this convention as well). A literal is any value that is expressed directly in your code, as opposed of being expressed via a variable or named constant. The number 12 and the string "Hello There" that I used in previous examples are also literals. By enclosing the date/time in # signs rather than quotes, we are telling VBScript to treat the value as a date, not as a string. As a result when the date literal gets stored in the variant variable, the subtype comes out as Date.

Here is another example to force date/time instead of String.

Dim varTest
varTest = CDate("5/16/99 12:30 PM")
MsgBox TypeName(varTest)

This example uses the CDate ( ) conversion function to achieve the same thing. Once again, the first version is theoretically faster since it does not require an extra function.

First
Next