FormatDateTime() displays a VBScript Date or time using one of five named formats. The result depends on the ASP locale and regional settings, so it is best for human-readable presentation rather than storing a canonical date string.
<%
Dim my_date, previousLocale
previousLocale = SetLocale(1033) ' English (United States)
my_date = DateSerial(2006, 7, 19) + TimeSerial(20, 38, 39)
Response.Write FormatDateTime(my_date, vbGeneralDate)
SetLocale previousLocale
%>
Under typical US English regional settings, this resembles 7/19/2006 8:38:39 PM. The exact presentation is environment-dependent, even when you choose a locale.
FormatDateTime(date[, namedFormat])
| Parameter | Meaning |
|---|---|
date | Date or time expression to display |
namedFormat | Optional constant from vbGeneralDate through vbShortTime |
| Value | Constant | Meaning |
|---|---|---|
| 0 | vbGeneralDate | General date/time format |
| 1 | vbLongDate | Long localized date |
| 2 | vbShortDate | Short localized date |
| 3 | vbLongTime | Long localized time |
| 4 | vbShortTime | Short regional time; often shown without seconds |
<%
Dim currentDate
currentDate = Now
Response.Write FormatDateTime(currentDate, vbGeneralDate)
%>
The output is dynamic because Now uses the server’s current date and time. For predictable testing, use DateSerial() and TimeSerial().
<%
Dim sampleDate, formatType
sampleDate = DateSerial(2006, 7, 19) + TimeSerial(20, 38, 39)
For formatType = 0 To 4
Response.Write formatType & ": " & FormatDateTime(sampleDate, formatType) & "<br>"
Next
%>
The five lines correspond to general, long date, short date, long time and short time. Text output varies by server locale, so avoid hard-coding locale-dependent expected strings.
<%
Dim myDate
myDate = DateSerial(2026, 11, 13)
Response.Write FormatDateTime(myDate, vbLongDate)
%>
This is appropriate for a localized page heading or confirmation message when the server’s date locale is configured as intended.
<%
Dim previousLocale, myDate
myDate = DateSerial(2026, 11, 13)
previousLocale = SetLocale(1033) ' US English
Response.Write FormatDateTime(myDate, vbLongDate)
SetLocale previousLocale
%>
Restore the previous locale when your script finishes displaying the date. Changing locale does not convert a time from one time zone to another.
When an exact numeric format is required—for a machine-readable field, filename or data transfer—assemble the components instead of relying on vbShortDate:
<%
Dim myDate, isoText
myDate = DateSerial(2026, 11, 13)
isoText = Year(myDate) & "-" & Right("0" & Month(myDate), 2) & "-" & _
Right("0" & Day(myDate), 2)
Response.Write isoText
%>
2026-11-13
<%
Dim sampleTime
sampleTime = DateSerial(2026, 11, 13) + TimeSerial(14, 5, 9)
Response.Write FormatDateTime(sampleTime, vbLongTime) & "<br>"
Response.Write FormatDateTime(sampleTime, vbShortTime)
%>
Pass a valid Date value. For user-provided strings, validate and convert them first using IsDate() with awareness of locale, or construct dates from verified numeric components with DateSerial().
No. Regional date formats differ. Build the required components explicitly when you need fixed numeric ordering.
No. It changes regional interpretation and formatting, not the instant represented by the date/time value.
Use WeekdayName() with Weekday().
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| pranajl | 27-12-2012 |
| How to dispalyed Current date & time in ASP program | |