Retrieving Specific Portions of a Date and Time Value

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

Often times you are interested in only a portion of a date or time. For example, you might have a backup script that performs a full backup on Sundays and a partial backup every other day of the week. Likewise, you might have a script that retrieves event log events every day, but on the 15th and 30th of each month also clears the event log.

VBScript provides two ways to retrieve specific portions of a date or time. The DatePart function is a generic function that can retrieve any portion of a date or time value. In addition, VBScript also includes several functions, such as Day, Month, and Year, that allow you to retrieve a specific part of a date or time.

The DatePart function can be used to return a specific part of a date or time value. This function requires two items: the date to be parsed and one of the parameters shown in Table 2.7.

Table 2.7   DatePart Parameters

ParameterDescription
yyyyYear. Returns the year from the date-time value.
qQuarter. Returns the quarter 1, 2, 3, or 4 from the date-time value.
mMonth. Returns the month of the year using the following values:

1 January

2 February

3 March

4 April

5 May

6 June

7 July

8 August

9 September

10 October

11 November

12 December

yDay of Year. Returns the day number, with January 1 being 1 and December 31 being 365 (366 during leap years). For example, February 1 returns 32 because it is the 32nd day of the year.
dDay. Returns the day of the month. For example, both April 17 and August 17 return 17.
wWeekday. Returns the day of the week using the following values:

1 Sunday

2 Monday

3 Tuesday

4 Wednesday

5 Thursday

6 Friday

7 Saturday

You can specify that the day of the week start on a day other than Sunday. For more information, see "Retrieving Specific Portions of a Date and Time Value" later in this chapter.

wwWeek of Year. Returns the week number, with the week of January 1 typically being week 1 and the week of December 31 being week 52. However, you have several options for specifying which is the first week in the year. These options, in turn, affect the other week numbers. For details, see "Retrieving Specific Portions of a Date and Time Value " later in this chapter.
hHour. Returns the hour from the date-time value using 24-hour format. For example, 2:00 P.M. is returned as 14, and 6:00 P.M. is returned as 18. Times between midnight and 1:00 A.M. are returned as 0. Midnight (12:00 A.M.) is also returned as 0.
nMinute. Returns the minutes from the date-time value.
sSecond. Returns the seconds from the date-time value.

To use the DatePart function, you can create a variable and assign it the DatePart value. For example, the following line of code extracts the year from the current date and assigns it to the variable CurrentYear:

CurrentYear = DatePart("yyyy", Date) 

In the preceding example, the two parameters are:

  • "yyyy Indicates that the year should be returned from the specified date. This parameter must be enclosed in quotation marks.
  • Date Indicates that the date to be parsed should be the current date. You can also enclose a valid date within quotation marks (for example, "6/1/2002") or use a variable that has been assigned a date. For example, these two lines of code return the value 1977:
    DateToCheck = #8/15/1977# CurrentYear = DatePart("yyyy" , DateToCheck) 

Note

  • When assigning a date to a variable, enclose the date using date literals (#). This ensures that VBScript views the value as a date, and not as a number or string. Alternatively, you can use the CDate function.

The script shown in Listing 2.15 parses the current date and time, and then displays each date time component.

Listing 2.15   Using the DatePart Function

1 2 3 4 5 6 7 8 9 10 11 
Wscript.Echo Now Wscript.Echo "Year: " & DatePart("yyyy" , Now) Wscript.Echo "Quarter: " & DatePart("q", Now) Wscript.Echo "Month: " & DatePart("m" , Now) Wscript.Echo "Day of Year: " & DatePart("y" , Now) Wscript.Echo "Day: " & DatePart("d" , Now) Wscript.Echo "Weekday: " & DatePart("w" , Now) Wscript.Echo "Week of Year: " & DatePart("ww" , Now) Wscript.Echo "Hour: " & DatePart("h", Now) Wscript.Echo "Minute: " & DatePart("n" , Now) Wscript.Echo "Seconds: " & DatePart("s" , Now)

When the preceding script was run on January 17, 2002, at 11:40:27 A.M., the following output was returned:

1/17/2002 11:40:27 AM Year: 2002 Quarter: 1 Month: 1 Day of Year: 17 Day: 17 Weekday: 5 Week of Year: 3 Hour: 11 Minute: 40 Seconds: 27 

No error will be generated if you pass incomplete data to DatePart, but you might not get the expected results. For example, this line of code returns the value 1899:

Wscript.Echo DatePart("yyyy", "8:00 AM") 

This line of code returns 0:

Wscript.Echo DatePart("h", "12/1/2002") 

Configuring DatePart Options

By default, VBScript considers the first week of the year to be the week in which January 1 occurs. However, the DatePart function includes an optional parameter that can be used to set the first week of the year to one of the values shown in Table 2.8.

Table 2.8   Parameters for Setting the First Week of the Year

ConstantValueDescription
vbUseSystem0Uses the National Language Support API to determine the first full week based on the regional and language settings.
vbFirstJan11Sets the first week as the week in which January 1 occurs.
vbFirstFourDays2Sets the first week as the first week to have at least four days in it.
VbFirstFullWeek3Sets the first week as the first week that begins on a Sunday.

To use this parameter, call the DatePart function followed by the "ww" parameter, the date for which the week number is being determined, and the appropriate constant. The following script shows how the different constants can affect the week number assigned to a particular date.

TestDate = "1/6/2003" Wscript.Echo ProjectedDate Wscript.Echo "Week of Year: " & DatePart("ww" , TestDate) Wscript.Echo "Week of Year: " & DatePart("ww" , TestDate, vbFirstJan1) Wscript.Echo "Week of Year: " & DatePart("ww" , TestDate, vbFirstFourDays) Wscript.Echo "Week of Year: " & DatePart("ww" , TestDate, vbFirstFullWeek) 

When the preceding script runs under CScript, the following output appears in the command window:

1/6/2003 Week of Year: 2 Week of Year: 2 Week of Year: 2 Week of Year: 1 

As shown in Figure 2.13, January 6, 2003, does not fall in the same week as January 1, nor does it fall in the first week to have four days. However, it does fall during the first week to start with a Sunday.

Figure 2.13   January 6, 2003

January 6, 2003

Other Functions for Retrieving Portions of a Date

In addition to the DatePart function, the functions shown in Table 2.9 can also retrieve portions of a date-time value. DatePart can retrieve values such as day of the year and week of the year that cannot be retrieved any other way. However, DatePart does have parameters, such as "n" for minute, that are difficult to remember and can make your code harder to read and maintain than the stand-alone equivalent. For example, although these two lines of code return the same value (the minute), the second line leaves little doubt as to its purpose:

Wscript.Echo DatePart("n", Now) Wscript.Echo Minute(Now) 

Table 2.9   Functions for Retrieving Portions of a Date

FunctionDescription
DayReturns the day for the specified date-time value.
HourReturns the hour for the specified date-time value.
MinuteReturns the minute for the specified date-time value.
MonthReturns the month for the specified date-time value.
SecondReturns the second for the specified date-time value.
WeekdayReturns the day of the week for the specified date-time value. Return values are:

1 Sunday

2 Monday

3 Tuesday

4 Wednesday

5 Thursday

6 Friday

7 Saturday

YearReturns the year for the specified date-time value.

To use one of the stand-alone functions, simply call the function, passing the appropriate date-time value as the sole parameter. For example, the following code passes the current date and time to each stand-alone date function:

CurrentDate = Now Wscript.Echo "Year: " & VbTab & VbTab & Year(CurrentDate) Wscript.Echo "Month: " & VbTab & VbTab & Month(CurrentDate) Wscript.Echo "Day: " & VbTab & VbTab & Day(CurrentDate) Wscript.Echo "Weekday: " & VbTab & Weekday(CurrentDate) Wscript.Echo "Hour: " & VbTab & VbTab & Hour(CurrentDate) Wscript.Echo "Minute: " & VbTab & Minute(CurrentDate) Wscript.Echo "Second: " & VbTab & Second(CurrentDate) 

When the preceding script is run using CScript, the following output appears in the command window:

Year:           2002 Month:          1 Day:            31 Weekday:        5 Hour:           14 Minute:         20 Second:         41 

send us your feedback Send us your feedback « Previous | Next »   


Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 635

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net