Microsoft® Windows® 2000 Scripting Guide
« Previous | Next »
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.
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:
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
|
|
When the script is run using Cscript, the following output is returned:
Date: 1/8/2002 Days Until July 1: 174
Note
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
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 Next Wscript.Echo DateDiff(
"s
", Start, Now)
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:
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
|
|
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
Send us your feedback | « Previous | Next » |