Working With Dates and Times in VBScript - Performing calculations
(Page 5 of 6 )
Primarily, VBScript provides two different functions for performing Date and Time calculations: DateAdd, which is used to change a date based on a specified interval; and DateDiff, which is used to find the interval between two dates.
WScript.Echo DateAdd("m", 1, Now)
WScript.Echo DateAdd("d", -9, Date)
Output:
10/5/2008 4:34:36 AM
8/27/2008
DateAdd returns a Date value. It accepts three parameters. The first is an interval string from Table 1 below; the second is a number indicating the number of intervals; and the third is a Date value.
Table 4: Date interval strings
“d” | Day |
“m” | Month |
“yyyy” | Year |
“h” | Hour |
“n” | Minute |
“s” | Second |
“w” | Weekday |
“q” | Quarter |
“y” | Day of Year |
“ww” | Week of Year |
You can see in the example above how I’ve used DateAdd to return dates based on today. The first example returns the date one month from today. The second example uses a negative value to return the date nine days ago.
WScript.Echo DateDiff("d", "1-Apr-64", Now)
Output:
16228
The DateDiff function returns the number of intervals between two dates. It has three required parameters: the first is an interval string from Table 1 above, while the second and third are the two Date values to compare. It accepts the same optional parameters as the DatePart function you saw earlier in this article. In the example above, I’m using DateDiff to calculate the number of days between today’s date and April 1, 1964.
WScript.Echo #2-Feb-2008# - #20-Jan-2008#
WScript.Echo Date - #1-Jan-2008#
Output:
13.000000
248.000000
Finally, VBScript allows you to perform calculations on Dates directly. If you recall, Dates are treated internally as Doubles. As a result, VBScript will let you perform arithmetic calculations on them.
Because the Date data type is a numeric type, Dates can be used in any of VBScript’s mathematical functions.
Be aware that the result of arithmetic calculations between two or more dates will be of type Double.
WScript.Echo Int(Now)
WScript.Echo Fix(Now)
Output:
9/5/2008
9/5/2008
While Dates are used as Doubles for numeric calculations, I did notice one interesting behavior. VBScript’s Int and Fix functions are used to return the integer portion of a floating number. In both cases, VBScript correctly removes the time portion of a date, but returns the value as a Date instead of a Double. Apparently, VB correctly recognizes that we supplied a Date value and kindly sends one back. This got me wondering about VBScript’s behaviors.
dtmNow = Now
WScript.Echo dtmNow - Int(dtmNow)
WScript.Echo dtmNow - Fix(dtmNow)
Output:
0.206192
0.206192
I then decided to see what would happen if I attempted to perform a calculation with those results. In these examples, I’m using Int and Fix to return the date portion of the current date. I’m then subtracting that from the result of the Now function which should return the current time. In both cases, VBScript seems to return a Double.
WScript.Echo TypeName(dtmNow - Int(dtmNow))
Output:
Double
A quick check reveals that it does in fact return a Double. So how about accuracy?
WScript.Echo CDate(dtmNow - Int(dtmNow))
Output:
4:56:55 AM
Sure enough, VBScript’s calculation correctly returns the current time. This further proves how VBScript handles dates internally as numbers.
Next: Date formats >>
More Windows Scripting Articles
More By Nilpo