Including the date and/or time on a web page can be a subtle yet valuable addition when designing a web site. The addition of the date to the home page can create the impression that a site is constantly being updated with new content since each time a visitor loads the page, the current date will be displayed.
In this tutorial I’ll teach you how to add the date and time to your ASP pages using the VBScript FormatDateTime() function. I’ll explain how the function works, teach you how to integrate it into your ASP pages and illustrate the output you’ll get depending on the arguments you pass. I’ll round out the tutorial of the FormatDateTime() function by covering a few limitations that it has, which might or might not be a big deal depending on your specific needs.
Just FYI, this article assumes you know basic HTML and how to add ASP scripts to your web pages.
The FormatDateTime() Function
Microsoft provides a ton of predefined VBScript functions designed to reduce coding time. The FormatDateTime() function is one of those powerful functions and is really easy to use, too. This function uses the following format:
There are two arguments the function accepts: date and format. Table 1-1 below describes these arguments in greater detail:
Table 1-1: The FormatDateTime() function and its arguments
||This argument is required and can be any valid date expression such as Date or Now
||This format constant or format value specifies how the date and/or time will be displayed on your ASP page.
When specifying the format argument, you can either type the Visual Basic constant name (name in left column), or the constant’s corresponding value (0 – 4, from the middle column). They do the same thing, it’s just less typing if you use the value.
|This is the default. Not specifying a value or specifying 0 will produce a date in the format of mm/dd/yy.
If the date expression is Now, it will also return the time, after the date, in hh:mm:ss PM/AM format.
|This is my personal favorite 🙂 Passing this value will produce a date in the format of
weekday, month day, year*
* The year is Y2K compliant :-).
|Passing this value returns a date formatted just like the default of 0 (mm/dd/yy).
|Passing this value returns the time in hh:mm:ss PM/AM format.
|Passing this value returns military time in this format hh:mm
Table 1-1 is a good reference once you’ve got a feel for how the FormatDateTime() function works or if you’re an experienced programmer. For those of you that aren’t clear on how all the information in table 1-1 relates to “real world” implementations, let’s take a look at some examples:
Returning the Current Date
If you would like to display the current date, here are a few different ways to do it along with the results they produce:
<%= FormatDateTime(Date) %> returns: 7/14/2012
(You would get the same result by coding this: <%=FormatDateTime(Date, 0) %>)
<%= FormatDateTime(Date, 1) %> returns: Friday, July 14, 2012
<%= FormatDateTime(Date, 2)%> returns: 7/14/2012
Returning the Current Time
If you would like to return the current time, here are a couple of ways to do that:
<%= FormatDateTime(Now, 3)%> returns: 6:42:31 AM
<%= FormatDateTime(Now, 4)%> returns: 06:42
Returning the Current Date and Time
If you would like to return the current date and time together, here’s how to do just that:
<%= FormatDateTime(Now) %> returns: 7/14/2012 6:42:31 AM
If you’re like me, you probably don’t like the way the date and time displays above; it’s not very cool looking, is it? In cases like this, you can actually include two FormatDateTime() functions next to each other, in order to get the date and time in a more desriptive format, like this:
<%= FormatDateTime(Date, 1) %> <%= FormatDateTime(Now, 3)%> returns:
Friday, July 14, 2012 6:42:31 AM
Adding the code to your page
Integrating the code into your ASP pages is really easy; here is how the code would look on a page with basic HTML to display the date:
<title>Here’s the date</title>
Thank you for coming to this page. The current date is: <%= FormatDateTime(Date, 1) %>
Limitations of the FormatDateTime() function
The FormatDateTime() function is an extremely handy bit of code that can help you add a touch of flair almost instantly. I would like to mention four limitations that stick out in my mind, which may be an issue to you (or your clients) depending on the project at hand:
Limitation 1: The unneeded zero
On the first through ninth days of a month the day shows up in the format of “Month 01, Year”. I know it seems like a small thing but trust me, it’s can be a big deal to some.
Limitation 2: Only basic formatting allowed
You are limited to basic formatting of the string that’s returned by the FormatDateTime() function. Since the date and/or time function returns is a single string, you can bold, italicize and change the whole date/time by adding HTML or style sheet tags around it, but you can’t change the display properties for a single part (e.g., the month).
With the FormatDateTime() function, you can do this:
<b><%= FormatDateTime(Date, 1) %></b> which would return this: Friday, July 14, 2012
But you can’t do this:
Wednesday, January 1, 2012
If limitations 1 or 2 are a major hang up for you, you’ll need to use different ASP/VBScript techniques to add the date to your page. I’ll cover those in my next article!
Limitation 3: The time isn’t necessarily “their” time.
Limitation 3 is more of a by-product than a limitation, but I figured I would keep the naming conventions the same for this section. If you use the the FormatDateTime() function (or any other date related function) on the server side, the date/time returned will be whatever the server’s date and time is, not your client’s time from their system.
Limitation 4: It’s static.
Think of the displayed date or time as a “snap shot” of when the page was requested by the visitor. You cant use this function to display a “clock” that updates every second, or automatically update the date on the page when one day turns to the next.
I hope you’ve enjoyed this article on the FormatDateTime() function. I’ll be back soon with more date and time related ASP fun!