Rated out of 5 stars (4.1154) - 26 Total Votes
Monday, 30 November, 2020
In this article I will Show you how to display the Date and time on your web page with CLASP!
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: 11/30/2020 4:33:42 AM
If we just want to show the standard Date by itself we do this:
<%= date %>
Which gives us this: 11/30/2020
Or the Time by itself:
<%= time %>
Will display this: 4:33:42 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:
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
|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: Monday, November 30, 2020
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: 04:33
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) %>
Once we have the number of the weekday we can get the name:
<%= WeekdayName(Weekday(date)) %>
To abbreviate it we add "true" to the function like this:
<%= WeekdayName(Weekday(date),true) %>
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) %>
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) %>
<%= MonthName(6,true) %>
We can get the year from the date by doing this:
<%= Year(date) %>
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) %>
= Monday, 30 November, 2020
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!
Steve Frazier has been a classic ASP developer since 2003. He has developed CLASP applications for Fortune 500 companies and popular website's. He has also developed many ASP Scripts of his own! He is Web-master of HTMLJunction as well as its sister site - ASP Junction. He is currently working on a Web Portal that has the functionality of all the most popular Forums and Portals.