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

Saturday, 27 April, 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:  4/27/2024 9:55:08 AM

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

                
<%= date %>
                
            

Which gives us this:  4/27/2024

Or the Time by itself:

                
<%= time %>
                
            

Will display this:  9:55:08 AM

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:

                
FormatDateTime(date,format)
                
            

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

or

                
<%= FormatDateTime(date,vbLongDate) %>
                
            

Which gives us this:  Saturday, April 27, 2024

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

                
<%= FormatDateTime(time,4) %>
                
            

or

                
<%= FormatDateTime(time,vbShortTime) %>
                
            

So we get this:  09:55

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

 = 7

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

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

 = Saturday

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

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

 = Sat

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

 = Apr

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

 = Saturday, 27 April, 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 w3schools.com, they are a great resource!