Working With Dates and Times in VBScript - Retrieving the date and time
(Page 2 of 6 )
VBScript provides three functions for retrieving the current system date and time. While the current date and time are considered a single Date value, these functions can return either or both parts. These values are based on the current system clock, so there can be extreme variances from system to system.
WScript.Echo Now()
WScript.Echo Date()
WScript.Echo Time()
Output:
9/5/2008 12:59:01 AM
9/5/2008
12:59:01 AM
The Now function returns the current system date and time. The Date and Time functions return only those portions, respectively. These functions return a Date data type. Internally, this is handled as a 64-bit signed floating point number. However, when displaying these values, VBScript will construct a string based on the local short-date and short-time formats as set by the system’s regional date and time settings.
On my machine, you can see that the United States region uses an MM/DD/YYYY short-date format and a 12-hour HH:MM:SS short-time format. The actual output you see may be different if you are using different regional date and time settings than I am.
WScript.Echo CDbl(Now)
WScript.Echo CDbl(Date)
WScript.Echo CDbl(Time)
Output:
39696.040972
39696.000000
0.040972
I mentioned that dates and times in VBScript are 64-bit signed floating point numbers. In VBScript these are represented by the Double data type. The code above is used to demonstrate what dates and times look like internally in VBScript. As you can see, they are in fact a floating point number. The integer portion is used to represent the date, while the decimal portion represents the time. Supplying a zero value to one of these parts will cause it to be ignored.
The current date and time is expressed as the number of days that have passed since midnight, December 30, 1899. At the time of this writing, 36,696 days have passed since that time. The time portion of this number expresses the current time at a fraction of a day.
WScript.Echo Timer()
VBScript also provides another useful function that is often little used. The Timer function returns a Double value based on the current system time that indicates the number of seconds that have passed since midnight. This can be used for performing calculations or timing events that require accuracy greater than a whole second.
Next: Working with date and time parts >>
More Windows Scripting Articles
More By Nilpo