Understanding Dates and Times in VBScript - Using the VT_Time Specification
(Page 4 of 4 )
As I stated previously, the VT_Time Specification represents dates and times using a 64-bit (8 bytes) signed floating point number. Because it is signed, the value may be positive or negative. VBScript has two numeric data types that fit this specification: Date and Double. Date was obviously created for this purpose. VBScript handles values of type Date in a way that allows you to display dates very easily. The numeric type Double can be used to display the actual 64-bit signed floating point number.
By default, Dates in VBScript will be displayed using the local short-date format. This is based on the local system settings.
VBScript supports any date between 1 Jan 100 AD and December 31, 9999 AD. In other words, it supports decimal values between -657434.000000 and 2958465.000000. These constraints were used for two reasons. First, this is pushing the limits available by the Double width, and second, because values less than 100 AD have a two digit year.
Attempting to use dates outside of the valid range will result in an error. Unfortunately, this error varies depending upon how this occurred. This makes debugging slightly difficult. My best advice: any date read from the system should be valid, but you should validate any date entered by a user before attempting to assign it to a date variable or function.
In the twentieth century, the MM/DD/YY date format was acceptable. Thus VBScript will accept two digits years, but it does assume that they occur between the years 1900 and 1999. Since this can provide unpredictable results, it’s best to always use four digit years.
VBScript dates are also considered to be local time. This means that they are calculated using the local Region and Time Settings. In other words, there is no inherent support for different time zones. Times are calculated based on the system’s current time zone. From a programming standpoint, this can be troublesome. Thus, any code that involves events occurring in separate time zones will need to have additional logic to handle the time zone changes.
For more detailed information about the shortcomings of VBScript’s dates and times, check out Eric Lippert’s blog on MSDN.
Now that you have an understanding of how dates and times are handled by VBScript, you’re ready to begin working with them in your code. In my next article, I’ll show you the various date and time functions available in VBScript. Until next time, keep coding!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |