Formatting Date and Time Values

Microsoft® Windows® 2000 Scripting Guide

By default, VBScript displays the output of the Now, Date, and Time functions using the same date-time settings used by the operating system. For computers running the American English version of Windows, output will look similar to that shown in Table 2.10.

Table 2.10 Default Date-Time Output

Function

Sample Output

Now

6/30/02 9:01:47 PM

Date

6/30/02

Time

9:01:47 PM

However, you are not limited to these default formats. Instead, VBScript includes several constants (shown in Table 2.11) that can be used along with the FormatDateTime function to display date-time values in alternate ways.

Table 2.11 FormatDateTime Settings

Constant

Value

vbGeneralDate

0

vbLongDate

1

vbShortDate

2

vbLongTime

3

vbShortTime

4

The following script displays the current date and time using each of the date-formatting constants:

CurrentDate = Now
Wscript.Echo "General date: " & FormatDateTime(CurrentDate, vbGeneralDate)
Wscript.Echo "Long date: " & FormatDateTime(CurrentDate, vbLongDate)
Wscript.Echo "Short date: " & FormatDateTime(CurrentDate, vbShortDate)
Wscript.Echo "Long time: " & FormatDateTime(CurrentDate, vbLongTime)
Wscript.Echo "Short time: " & FormatDateTime(CurrentDate, vbShortTime)

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

General date: 1/31/2002 3:29:37 PM
Long date: Thursday, January 31, 2002
Short date: 1/31/2002
Long time: 3:29:37 PM
Short time: 15:29

Although these are the only date-time formats predefined in VBScript, you can use the various date and time functions to create your own formats. For example, the following lines of code display the date using just the month and year (8/2002):

Wscript.Echo Month(Now) & "/" & Year(Now)

Formatting Days of the Week and Months of the Year

The WeekdayDay and Month functions allow you to retrieve just the day of the week or just the month from a specified date. When you use these functions, the data is returned as an integer. These values are summarized in Table 2.12.

Table 2.12 Weekday and Month Return Values

Value

Day of the Week

Month

1

Sunday

January

2

Monday

February

3

Tuesday

March

4

Wednesday

April

5

Thursday

May

6

Friday

June

7

Saturday

July

8

 

August

9

 

September

10

 

October

11

 

November

12

 

December

Both WeekdayName and MonthName require you to first use the Day or Month function to retrieve the integer value for the day or the month. You cannot use either WeekdayName or MonthName directly with a date. For example, this code will fail because the function cannot determine the integer value to use for the month:

Wscript.Echo MonthName("9/19/2002")

To determine MonthName, you must first use the Month function:

MonthValue = Month("9/19/2002")
Wscript.Echo MonthName(MonthValue)

Alternatively, you can nest the two functions in a single line of code:

Wscript.Echo MonthName(Month("9/19/2002"))

The WeekdayName and MonthName functions are described in more detail in Table 2.13.

Table 2.13 Date Formatting Functions

Function

Description

WeekdayName

Returns the actual name of the day rather than the number of the day. (For example, WeekdayName returns the value Tuesday rather than the value 3.) WeekdayName requires only one parameter, the day of the date being evaluated. This means you must nest the WeekdayName and Day functions like this:

Wscript.Echo WeekDayName(Day("9/1/2002"))

The preceding code returns the string "Sunday" because September 1, 2002, occurred on a Sunday. WeekdayName also accepts an optional parameter to abbreviate the name of the day. By setting this parameter to True, WeekdayName will return a three-letter abbreviation for the day. For example, this command returns the value "Sun":

Wscript.Echo WeekDayName(Day("9/1/2002") True) 

MonthName

Returns the actual name of the month rather than the number of the month. (For example, MonthName returns the value September rather than the value 9.) MonthName requires only one parameter, the month of the date being evaluated. This means you must nest the MonthName and Month functions like this:

Wscript.Echo MonthName(Month("9/1/2002"))

The preceding code returns the string "September". MonthName also accepts an optional parameter to abbreviate the name of the month. When this parameter is set to True, MonthName will return a three-letter abbreviation for the month. For example, this command returns the value "Sep":

Wscript.Echo MonthName(Month("9/1/2002") , True)