ASP Junction Home Of EZCodes
5 visitors online
149828 total visitors

Sunday, 1 August, 2010
Home Downloads Articles Programming Portfolio Support Privacy  




Newsletter


The HTMLJunction Store

Top 5 Downloads
EZNewsletter: 11326
EZGallery: 6518
EZPoll: 6375
EZUpload: 5818
EZGuestbook: 4486
Download Stats
EZHomepagePro: 3632
EZHomepageBasic: 1573
EZNewsletter: 11326
EZOnlineEditor: 4032
EZMedia: 3776
EZGuestbook: 4486
EZGallery: 6518
EZPoll: 6375
EZScheduler: 2432
EZUpload: 5818
Total: 50684
HTMLJunction News
ASP Article - Dynamic Date Dropdown Menu.

ASP Article - Import Text File to an Access Database.

EZScheduler now available for download!

ASP Junction has just been launched!

HTMLJunction has a new store!


Advertisements






HTMLJunction is against any kind of spyware!




ASP Article
Rated out of 5 stars - 25 Total Votes Submit Your 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!

There are various ways to display Dates and Times.  You can display the Date like at the top right of this page 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.

About the Author

Steve Frazier has been a classic ASP developer for about four years. He has developed ASP applications for Fortune 500 companies and popular website's. He has also developed many ASP Scripts of his own! He is Webmaster of HTMLJunction as well as its sister sites. The HTMLJunction Store - ASP Junction. He is currently working on a Web Portal that has the functionality of all the most popular Forums and Portals.



The simplest way to display the Date and Time is thus:  <%= now %>

Which gives us this:  8/1/2010 2:16:15 AM

If we just want to show the standard Date by itself we do this:  <%= date %>

Which gives us this:  8/1/2010

Or the Time by itself:  <%= time %>

Will display this:  2:16:15 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:  Sunday, August 01, 2010

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:  02:16

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

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

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

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

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

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

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

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 the page is written like this:

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

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!

I hope you found this article useful!

Rate this Article
Good  
5 4 3 2 1
  Poor

Advertisements








We use Google Sitemaps to inform Google's crawler about all your pages and to help people discover more of your web pages.

Advertisements






Resources

Gunners Gallery
Wesley Ford
seo-advantage.com

copyright © 2010 ASP Junction
An HTMLJunction website