Format date and time with FormatDateTime()

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.

On this page — Show page sectionsHide page sections

Syntax

FormatDateTime(date[, namedFormat])
ParameterMeaning
dateDate or time expression to display
namedFormatOptional constant from vbGeneralDate through vbShortTime

Named format constants

ValueConstantMeaning
0vbGeneralDateGeneral date/time format
1vbLongDateLong localized date
2vbShortDateShort localized date
3vbLongTimeLong localized time
4vbShortTimeShort regional time; often shown without seconds

Display the current date and time

<%
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().

Compare every named format

<%
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.

Display a full date name

<%
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.

Explicitly select and restore locale

<%
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.

Build an invariant YYYY-MM-DD string

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

Show a localized time

<%
Dim sampleTime
sampleTime = DateSerial(2026, 11, 13) + TimeSerial(14, 5, 9)
Response.Write FormatDateTime(sampleTime, vbLongTime) & "<br>"
Response.Write FormatDateTime(sampleTime, vbShortTime)
%>

Use FormatDateTime() safely

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().

Frequently asked questions

Will vbShortDate always output MM/DD/YYYY?

No. Regional date formats differ. Build the required components explicitly when you need fixed numeric ordering.

How do I show only a month name?

Use MonthName(Month(myDate)).

Does SetLocale change the server time zone?

No. It changes regional interpretation and formatting, not the instant represented by the date/time value.

How do I show a weekday?

Use WeekdayName() with Weekday().

Back to top ↑


ASP Home



pranajl

27-12-2012

How to dispalyed Current date & time in ASP program



✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer