Date Arithmetic

Microsoft® Windows® 2000 Scripting Guide

Individual dates are an important consideration is system administration. Equally important, however, are ranges of dates and intervals between dates. In system administration, you will often need to make projections based on a specified date and a specified interval. For example, you might need to know which date is 180 days from today. Alternatively, you might have two dates or times and need to calculate the difference between them. If a service started on February 1, 2002, at 6:00 A.M. and then shut down on July 17, 2002, at 3:30 P.M., you might want to calculate how long the service ran before stopping.

VBScript provides two functions, DateDiff and DateAdd, which enable you to perform date arithmetic.

Determining the Interval Between Two Dates or Times

Calculating the amount of time that elapsed between two events is a common system administration task. For example, using WMI, you can determine the date and time that a computer started. By subtracting that value from the current date and time, you can calculate the system uptime, the amount of time the computer has been running since its last reboot.

The DateDiff function is used to calculate time intervals such as this. DateDiff requires three parameters:

  • The date or time interval (for example, the number of days between two events or the number of hours between two events). DateDiff accepts the same date parameters as DatePart. These parameters are shown in Table 2.7.

  • The date of the first event.

  • The date of the second event.

For example, the script shown in Listing 2.16 calculates the number of days between the current date and July 1, 2002. To calculate a different interval, simply substitute the appropriate parameter. For example, to calculate the number of weeks between the two dates, replace the "d" parameter with the "w" parameter.

Listing 2.16 Determining Time Intervals


Wscript.Echo "Date: " & Date 
Wscript.Echo "Days Until July 1: " & DateDiff("d", Date, "7/1/2002")

When the script is run using Cscript, the following output is returned:

Date: 1/8/2002 
Days Until July 1: 174

note Note

  • Depending on the dates you use, you might get a negative number. For example, the difference between January 18, 2002, and July 1, 2002, is 164 days. If you reverse the order of the two dates, however, DateDiff will return the value 164. If you do not want negative values, you can use Abs, the VBScript absolute value function: Abs(DateDiff("d", "7/1/2002", "1/18/2002"). The absolute value function always returns a positive (unsigned) value.

With DateDiff, you might get different results depending on whether you use the "w" or the "ww" parameter. The "w" parameter calculates the number of weeks between the two dates, based on the day of the week when the first date occurs. For example, January 18, 2002, occurred on a Friday. With the "w" parameter, DateDiff calculates the number of Fridays between the two dates, not counting the initial Friday. For the interval January 18, 2002, to July 1, 2002, the "w" parameter counts 23 weeks.

The "ww" parameter, however, counts the number of Sundays between the two dates, again not counting the initial date. In this case, there are 24 Sundays between the two dates, meaning that the "w" and "ww" parameters return different values.

note Note

  • You might also get unexpected results when calculating the number of years between events. For example, the time between December 31, 2001, and January 1, 2002, is 1 day. However, because it covers two different years, DateDiff will tell you that there is 1 year between the two days. Because of that, you might want to return the number of days between two events and then divide by 365 to determine the number of years between the two.

DateDiff is also a useful tool for monitoring how long it takes for your scripts to run. To time the running of a script, set a variable to Now before the script runs any code. This variable will thus hold the time that the script started. At the end of the script, subtract this value from Now, and echo the results.

For example, the following script tracks how long it takes to retrieve and display the free disk space for all the disk drives on the local computer.

Start = Now 
Set objWMIService = GetObject("winmgmts://") 
Set colLogicalDisk = objWMIService.InstancesOf("Win32_LogicalDisk") 
For Each objLogicalDisk In colLogicalDisk 
    Wscript.Echo objLogicalDisk.DeviceID & " " & objLogicalDisk.FreeSpace 
Wscript.Echo DateDiff("s", Start, Now)

Projecting Dates Based on a Specified Time Interval

DateDiff can tell you the interval between two dates; however, there will be times when you know the interval but need to calculate either the starting date or the end date. For example, suppose you want to retrieve all the event log events that were recorded in the past 45 days. You know the interval: 45 days. You also know the end date: today. What you do not know is the beginning date.

The DateAdd function projects a date forward or backward from a specified starting point; in a sense, it is almost the reverse of the DateDiff function. With DateAdd, you specify the following three parameters:

  • The time interval (again using the values shown in Table 2.7). For example, to calculate 180 days from today, use the "d" parameter.

  • The time value (for example, 180 to indicate 180 days). To work backward from the starting date (for example, to determine the date 45 days ago), use a negative number. To project forward in time, use a positive number.

  • The starting date.

For example, the script shown in Listing 2.17 calculates the date that falls 180 days from the current date.

Listing 2.17 Projecting Dates


Wscript.Echo "Date: " & Date 
Wscript.Echo "180 Days From Today: " & DateAdd("d", 180, Date)

When the script shown in Listing 2.17 was run on January 8, 2002, the following output was returned:

Date: 1/8/2002 
180 Days From Today: 7/7/2002