Working with Dates and Times
Dates and times allow your scripts to interact more precisely with the real world. You can copy or move files based on their "last changed" date, delete users based on the last time they logged on, and so forth. Next to strings and numbers, dates and times are the third most common data type that you'll use in your scripts.
Dates and Times in VBScript
VBScript stores dates and times in a serial number format that looks like a large decimal number. The serial number counts the number of milliseconds that have elapsed since January 1, 100 C.E., and can represent dates and times up to December 31, 9999. The integer portion of a date serial number-the portion before the decimal point-is used to represent days (and thus, months and years), whereas the fractional portion-the part after the decimal point-represents milliseconds (and seconds, minutes, and hours).
VBScript includes a number of functions for working with dates and times. For example, the DatePart() function analyzes a date and returns just the specified part of it. DatePart("yyyy", Date()), for example, returns the year portion of the current date. DatePart() accepts a number of different strings, which tell it which portion of the date you're interested in.
"yyyy" returns the year. "q" returns the quarter of the year. "m" returns the month. "y" returns the Julian date, which is the number of days that have elapsed since the beginning of the year. "d" returns the day as a number. "w" returns the weekday, such as "Monday." "ww" returns the week of the year. "h" returns the hour. "n" returns the minute. Don't confuse this with "m," which actually returns the month. "s" returns the second.
The second parameter of DatePart() can be anything VBScript recognizes as a date or time, including string variables that contain date or time information, such as "1/1/2004" or "10:26 P.M."
Getting the Date or Time
VBScript has a number of functions that return the current date or time, or portions thereof.
Date() returns the current date. Day() returns the current day, numbered 1 to 31. Now() returns the current date and time. Month() returns the current month, numbered 1 to 12. Year() returns the current year. Weekday() returns the current day of the week, numbered 1 to 7. Time() returns the current system clock time. Hour() returns the current hour, numbered 0 to 23. Minute() returns the current minute of the system clock. Second() returns the current second of the system clock.
There are a couple of additional functions used to turn numeric date data, such as month or day numbers, into strings.
MonthName() returns the name of the month. For example, MonthName(1) returns "January." MonthName(1,True) returns "Jan," the abbreviated form of the month name. WeekdayName() returns the name of a day of the week. WeekdayName(2) returns "Monday," whereas WeekdayName(2,True) returns the abbreviated "Mon." Sunday is the default first day of the week.
Converting Date and Time Data
You can convert data to a date or time by using the CDate() function. For example, CDate("1/1/2004") will convert the string value "1/1/2004", which looks like a date, into the corresponding date serial number. It's difficult to get VBScript to display the internal serial number, and an example like the following simply displays something that looks like a normal date.
dDate = CDate("1/1/2004")
MsgBox dDate
MsgBox Date()
When VBScript executes the MsgBox statements, it redisplays the dates in whatever format your computer is configured to use based on its region settings.
You can also generate date or time data from individual date or time components, by using the DateSerial() and TimeSerial() functions. For example, DateSerial(2004, 5, 12) will return the date 5/12/2004. Similarly, TimeSerial(5, 23) will return 5:23 A.M.
Working with Past and Future Dates
VBScript provides the DateAdd() function, which allows you to perform math with dates and times. DateAdd() requires three parameters: an interval, a number, and a starting date or time. Intervals can be the following:
"yyyy" for the year. "q" for the quarter of the year "m" for the month "y" for the Julian date, which is the number of days that have elapsed since the beginning of the year "d" for the day as a number "w" for the weekday, such as "Monday" "ww" for the week of the year "h" for the hour "n" for the minute "s" for the second
For example, DateAdd("yyyy", 1, "1/1/2004") will return 1/1/2005, which is the starting date plus one year. You can use DateAdd() to subtract by specifying a negative number: DateAdd("m", -1, "1/1/2004") will return 12/1/2003, removing one month from the starting date. The function is leap-year-aware, meaning that DateAdd("yyyy", 1, "2/29/2000") will not return 2/29/2001, because 2001 is not a leap year. The function will instead return 3/1/2001, which is 365 days after the starting date.
DateDiff() is a similar function that returns the difference between two dates. It accepts the same interval parameters as DateAdd(), and accepts two dates for comparison. DateDiff("d", "12/31/2002", "12/31/2003") will return 365, because that's the number of days between the two dates. If the first date specified is later than the second, the number returned will be negative.
|