Working With Dates and Times in VBScript - Working with date and time parts
(Page 3 of 6 )
You may not always wish to return the entire date or time. Your script may only need to know the current month or day of the week. As a result, VBScript provides a number of different functions for retrieving date and time parts.
dtmValue = Now()
WScript.Echo Hour(dtmValue)
WScript.Echo Minute(dtmValue)
WScript.Echo Second(dtmValue)
Output:
0
59
1
The most commonly used of these functions return portions of the current time. The Hour function returns a number between 0 and 24 that represents the current hour. Note that this number is independent of whether or not the system uses a 12 or 24-hour clock. The Minute and Second functions both return a value between 0 and 60, as you might expect. The Second function always returns a two-digit number, so it may contain a leading zero.
WScript.Echo Day(dtmValue)
WScript.Echo Month(dtmValue)
WScript.Echo MonthName(Month(dtmValue))
WScript.Echo Year(dtmValue)
Output:
5
9
September
2008
Similar functions exist for extracting parts of a date. The Day function returns a number between 1 and 31, indicating the day of the month; the Month function returns a number between 1 and 12, which represents the month of the year; and the Year function returns a number indicating the four-digit year. VBScript also provides the MonthName function for returning the textual month name. This function accepts a single parameter—an integer indicating the month. It returns a string that varies based upon the local system language.
WScript.Echo Weekday(dtmValue)
WScript.Echo WeekdayName(Weekday(dtmValue))
Output:
6
Friday
VBScript can also determine the weekday for a specified date. The Weekday function returns a number between 1 and 7 that indicates the current day of the week. The WeekdayName function can use this number to return the textual representation of that value. This string varies depending upon the current system language and local regional and time settings, which determine what day of the week is first on the calendar.
WScript.Echo DatePart("y", Now)
WScript.Echo DatePart("ww", Now)
Output:
249
36
Finally, VBScript provides another very interesting function. The DatePart function is used to return parts of a date, much like the functions you’ve just seen. It differs in its flexibility. It accepts two required parameters. The first is a string that indicates what interval (or date part) to return. The list of acceptable values can be found in Table 1 below. The second required parameter is a Date value. In the example above, I’ve used date part to return the number of days that have passed in the current year and the number of weeks, respectively.
Table 1: 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 |
DatePart also accepts two optional parameters. The first of these determines the day to be considered the first day of the week. If used, it accepts a constant value from Table 2 below. If omitted, this value defaults to vbSunday.
Table 2: VBScript WeekDay Constants
Constant | Value | Description |
vbUseSystemDayOfWeek | 0 | Use NLS API (system) setting |
vbSunday | 1 | Sunday |
vbMonday | 2 | Monday |
vbTuesday | 3 | Tuesday |
vbWednesday | 4 | Wednesday |
vbThursday | 5 | Thursday |
vbFriday | 6 | Friday |
vbSaturday | 7 | Saturday |
DatePart optionally accepts a final parameter that specifies the first week of the year. It accepts a constant value from Table 3 below. If omitted, this defaults to vbFirstJan1.
Table 3: VBScript FirstWeekOfYear Constants
Constant | Value | Description |
vbUseSystem | 0 | Use NLS API (system) setting |
vbFirstJan1 | 1 | The week in which Jan 1 occurs. |
vbFirstFourDays | 2 | The first week having at least 4 days in the new year. |
vbFirstFullWeek | 3 | The first full week of the new year. |
Next: Assigning Date values >>
More Windows Scripting Articles
More By Nilpo