DateTo 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.
TimeTo 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 DisplayedYou 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 MonthnameTo 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.
DayTo 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.
YearTo display the current year, type: <% =year(now) %> where you want it to appear.
ExampleSuppose you wanted to display today's date as day/month/year instead of month/day/year. To do so, you would use the day, month, and year ASP functions together, by typing: <% =day(now) %>/<% =month(now) %>/<% =year(now) %> When you viewed the page, you would see something like this:
23/1/1997
Later we'll see how you can change this so only the last two digits of the year are displayed, like this:
23/1/97
Weekday and WeekdaynameTo display the day of the week as a number from 1 to 7 in a Web page, type: <% =weekday(now) %> where you want it to appear. When you view the page in Internet Explorer, you'll see a 1 if today is Sunday, 2 if it's Monday, and so on.
To display the day of the week by name, type: <% =weekdayname(weekday(now)) %> where you want it to appear.
Hour, Minute, and SecondTo display just the hour part of the current time, type: <% =hour(now) %> where you want it to appear. The hour function is based on a 24-hour clock. When you view the page, you'll see a number between 0 and 23.
To display just the minutes part of the current time, type: <% =minute(now) %> where you want it to appear. When you view the page, you'll see a number between 0 and 59.
To display just the seconds part of the current time, type: <% =second(now) %> where you want it to appear. When you view the page, you'll see a number between 0 and 59.
ExampleTry typing this into a Web page: The time is <% =time %>. That means it's <% =minute(now) %>
minutes past <% =hour(now) %> o'clock. When you view the page in Internet Explorer, you should see something like this:
The time is 1:36:05 PM. That means it's 36 minutes past 13 o'clock.
Remember, the hour function is based on a 24-hour clock. Later we'll see how to convert from the 24-hour clock to a 12-hour clock.
TimevalueYou probably won't ever use the timevalue function. It takes the different ways you can write the time, such as "2:24PM" and "14:24," and returns them in this format: "2:24:00 PM." This can be useful if you're using a function that needs to be given the time in that exact format.
ExampleEarlier in this section we saw how you can use the hour, minute, and second functions to break up the time into hours, minutes, and seconds. With the timevalue function, you can put them back together. Type this into a Web page: When it's 23 minutes and 5 seconds past 4 o'clock in the afternoon,
that means it's <% =timevalue("16:23:05") %>.
This is the same as <% =timevalue("4:23:05PM") %>
or <% =timevalue("16:23:05PM") %>.Make sure you type "16:23:05PM" and not "16:23:05 PM." The "05" and the "PM." should be run together, not separated by a space. When you view the page in Internet Explorer, you should see:
When it's 23 minutes and 5 seconds past 4 o'clock in the afternoon, that means it's 4:23:05 PM. This is the same as 4:23:05 PM or 4:23:05 PM. |