VBScript FormatDateTime Function

Definition and Usage

The FormatDateTime function can format and return a valid date or time expression.

Syntax

FormatDateTime(date, format)
Parameter Description
date Required. Any valid date expression. (For example, Date() or Now.)
format Optional. Specifies the format value of the date/time format used.

format parameter:

Constant Value Description
vbGeneralDate 0 Display the date and/or time. If there is a date part, display it in short date format. If there is a time part, display it in long time format. If both exist, display all parts.
vbLongDate 1 Display the date using the long date format specified in the computer's regional settings.
vbShortDate 2 Display the date using the short date format specified in the computer's regional settings.
vbLongTime 3 Display time using the format: hh:mm:ss PM/AM
vbShortTime 4 Display time using 24-hour format (hh:mm).

Example

Example 1

D = #2001/2/22#
document.write(FormatDateTime(D))

Output:

2001-2-22

Example 2

D = #2001/2/22#
document.write(FormatDateTime(D,1))

Output:

February 22, 2001

Example 3

D = #2001/2/22#
document.write(FormatDateTime(D,2))

Output:

2001-2-22

Example 4

D = #2001/2/22#
document.write(FormatDateTime(D,3))

Output:

2001-2-22