| Answer: |
Performing date-related calculations in VBScript is really a breeze thanks to the myriad of helpful date-related functions in the language. There have, in fact, been a wide array of articles on the many powerful VBScript date functions. For a quick rundown, check out:
-- Using Date Functions (Part 1) -- Using Date Functions (Part 2) -- Using VBScript's Date Function -- FAQ: How can I add or subtract time from a date?
One common task developer's want to do, is given two dates, calculate the number of years, days, months, or whatever between the two dates. Fortunately, VBScript provides a nice function to do this called DateDiff. (View the technical docs for DateDiff.) In its simplest form, DateDiff takes three input parameters:
* interval - specifies the date interval difference you are interested in (such as the months between two dates, or the weeks between dates, etc.) * date1 - the first date * date2 - the second date
The interval can be one of the following string values:
yyyy - Year
q - Quarter
m - Month
y - Day of year
d - Day
w - Weekday
ww - Week of year
h - Hour
n - Minute
s - Second
So if we have two dates, the current date and January 1, 2000, and we wanted to calculate the total number of days between the two, we could use the following code:
Dim dtNow, dtY2K dtNow = Date() dtY2K = DateSerial(2000, 1, 1)
Dim iDaysDifference iDaysDifference = DateDiff("d", dtY2K, dtNow)
|
This will return the total number of days that have elapsed since the two dates. That is, if dtNow was equal to Feb. 3rd, 2002, the result would be: 764.
|