ASP Article

Displaying Dates and Times with ASP

In this article I will Show you how to display the Date and time on your web page with ASP!.

Tuesday, 18 June, 2024

There are various ways to display Dates and Times.  You can display the date like at the top of this article or you can display it in the standard MM/DD/YYYY.  You can display the Date with the Time or the Time all by itself.  You can also abbreviate the name of the Day or display the Time in either 24 hour or 12 hour format.

The simplest way to display the Date and Time is thus:

<%= now %>

Which gives us this:  6/18/2024 9:17:37 PM

If we just want to show the standard Date by itself we do this:

<%= date %>

Which gives us this:  6/18/2024

Or the Time by itself:

<%= time %>

Will display this:  9:17:37 PM

There is a Function we can use to change the way the Date/Time is displayed and it's called FormatDateTime
The Syntax for the FormatDateTime function looks like this:


The date can be any date or Date Function and is required.
The format is a value that says what format to use and is optional.
The values and descriptions are listed below

Format Values
Constant Value Description
vbGeneralDate 0 Display a date in format mm/dd/yy. If the date parameter is Now(), it will also return the time, after the date
vbLongDate 1 Display a date using the long date format: weekday, month day, year
vbShortDate 2 Display a date using the short date format: like the default (mm/dd/yyyy)
vbLongTime 3 Display a time using the time format: hh:mm:ss PM/AM
vbShortTime 4 Display a time using the 24-hour format: hh:mm

Here are a few examples.

To display the Name of the day and the name of the month and the day and year we do this:

<%= FormatDateTime(date,1) %>


<%= FormatDateTime(date,vbLongDate) %>

Which gives us this:  Tuesday, June 18, 2024

If we want to show the time in the 24 hour format we can use this:

<%= FormatDateTime(time,4) %>


<%= FormatDateTime(time,vbShortTime) %>

So we get this:  21:17

We can also display the name of the weekday and month separately and even abbreviate them.

For the weekday we first have to get the number of the weekday and this is how we do it:

 <%= Weekday(date) %>

 = 3

Once we have the number of the weekday we can get the name:

<%= WeekdayName(Weekday(date)) %>

 = Tuesday

To abbreviate it we add "true" to the function like this:

<%= WeekdayName(Weekday(date),true) %>

 = Tue

We do the Month name in the same way, first we get the number of the month then we can get the name of the month and abbreviate it:

<%= MonthName(Month(date),true) %>

 = Jun

We can also replace Weekday(date) and Month(date) with the number of the weekday or month and get the same result.

<%= WeekdayName(4,true) %>

 = Wed

<%= MonthName(6,true) %>

 = Jun

We can get the year from the date by doing this:

<%= Year(date) %>

 = 2024

The Copyright notice at the bottom of the page is in an include file and it changes with the year so I never have to change it:

The date at the top of this article is written like this:

<%= weekdayname(weekday(date)) &", "& day(date) &" "& monthname(month(date)) &", " & year(date) %>

 = Tuesday, 18 June, 2024

You can do a lot with these ASP Functions like get the date of a holiday from a database and display it, we'll use US Independence day as an example:

We will use the Cdate() Function to convert a string variable into a Date variable.

    Dim datHoliday

    datHoliday = Cdate("7/4/1776")

    Response.Write MonthName(Month(datHoliday)) &" "& Day(datHoliday) &"th "& Year(datHoliday)

July 4th 1776

This article only covers the basics on working with Date/Time. If you would like to learn more I suggest you visit, they are a great resource!