What are Active Server Pages? - Displaying the Current Date and Time
(Page 2 of 6 )
The date and time described in this section are those that are on the server.
Date
To display the current date by itself in a Web page, type:
<% =date %>
at the point where you want it to appear. When you view the page in your browser, you should see something like this:
Thu, Jan 23, 1997
Note: Even though "=date" is a short script, it's actually made up of two parts. The "date" part tells the server, "Get me the date." The equal sign (=) tells the server to display the date in the Web page. If you typed just:
<% date %>
the server would get the current date from your system, but that's all. It wouldn't display it. There are times when it makes sense to use an ASP function without the equal sign.
Time
To display the current time by itself, type:
<% =time %>
where you want it to appear. When you view the page, you should see something like this:
4:19:46 PM
Now (Date and Time)
To display the current date and time, type:
<% =now %>
where you want them to appear. When you view the page, you should see something like this:
1/23/97 4:19:46 PM
Changing the Way Date and Time are Displayed
You can also use Active Server Pages (ASP) functions to customize the way the current date and time are displayed on your Web page. To do this, use the now function together with the following formatting functions.
Month and Monthname
To display the number of the current month in a Web page, type:
<% =month(now) %>
where you want it to appear. When you view the page in your browser, you'll see a 1 if the current month is January, 2 if it's February, and so on.
To display the name of the current month, type:
<% =monthname(month(now)) %>
where you want it to appear.
Day
To display the day of the current month, type:
<% =day(now) %>
where you want it to appear. When you view the page, you'll see a number between 1 and 31.
Year
To display the current year, type:
<% =year(now) %>
where you want it to appear.
Next: Example 1 >>
More ASP Articles
More By Sriram K