Formatting Dates & Time


Suppose you have a date that you would like to format. You can use the FormatDateTime
function to do this for you. Note that this is the time at the server, not necessarily the viewer's local time.

Let's assume that we're working with today's date. This makes it real simple.

For example, today is 02/15/2000.
The code is simply <% Date %>

Now, let's assume that we need the date and time: 02/15/2000 4:15:14 AM
The code: <% Now %>

Let's say, however, that we want to display the date like this: Tuesday, February 15, 2000.
The code: <% FormatDateTime(date,1) %>
The number after the variable for the date (in this case, date, but it could be any variable that represents a valid date) specifies the format of the date. For example, 1 displays the date as
shown above, and 2 displays it as a short date (02/15/2000).

You can format time the same way. For example, it is now 4:15:14 AM.
The code: <% FormatDateTime(Now,3) %>

If you want only the time, you can use the word Time.
The code: <% Time %>

Now let's assume we want to format the time a bit differently, without the seconds. For example,
it is now 04:15 in 24-hour time.
The code: <% FormatDateTime(Now,4) %>

Now let's see what happens when we use these numbers with Time:
<% FormatDateTime(Time,3)%> yields 4:15:14 AM
<% FormatDateTime(Time,4)%> yields 04:15

You can also break out hour, minutes and seconds if you need to.
<% Hour(Now) %> Yields 4, the current hour in 24 hour time.
<% Minute(Now) %> Yields 15, the minute of the current time.
<% Second(Now) %> Yields 14, the current second of the current time.

For information on working with parts of dates (month, day, year), please see the Month Day Year page. Related information on working with dates is located on the Date Calculations page.