What are Active Server Pages? - Cool Things You Can Do with Date, Time, and Text
(Page 5 of 6 )
Here are some examples of interesting things you can do with date, time, and text functions.
Link of the Day
What if you wanted to have a link that pointed to a different page every day of the week? Here's how you can do that. First, choose the pages (HTML files) on your Web site that you want your link to point to. Name them "Sunday.htm," "Monday.htm," and so on. (If you don't have seven different HTML files, you can copy some of the files or make aliases on your Macintosh to them. The important thing is that there has to be one file or alias for every day of the week.)
To make the link, type:
<a href= <% =weekdayname(weekday(now)) %>.htm>Link of the Day</a>
where you want it to appear. When you click this link in Internet Explorer, it will take you to today's page.
Another Way to Display Today's Date
Earlier we saw how to change the date display from month/day/year to day/month/year like this:
23/1/1997
We can also change the date display so only the last two digits of the year are included. To do this, type:
<% =day(now) %>/<% =month(now) %>/<% =Right((year(now)), 2) %>
Now when you view the page, you should see something like this:
23/1/97
Another Way to Display the Time
In an earlier example, we wrote a server-side script to display the current time in words such as: "The time is 36 minutes and 5 seconds past 13 o'clock." This script used the ASP hour function, which returns just the hour part of the current time, based on a 24-hour clock.
In this example, we'll see how to change 24-hour clock times such as "13 o'clock" to 12-hour clock times ("1 o'clock PM"). To do this, we'll need to make the server-side script that uses the hour function a little more complicated. Instead of:
<% =hour(now) %> o'clock
we'll need to write a script that looks at the hour and does one of the following:
If the hour is 0 (zero), the script displays "midnight."
If the hour is 12, the script displays "noon."
If the hour is between 1 and 11, the script doesn't change it, but it displays "AM" after "o'clock."
If the hour is between 13 and 23, the script subtracts 12 (to make it a number between 1 and 11) and displays "PM" after "o'clock."
The script is shown below. It isn't written quite the way a programmer would write it, but it works, and it's fairly easy to understand, since it follows the items in the bulleted list above exactly.
The hour is
<% if hour(now) = 0 then %>
midnight.
<% end if
if hour(now) = 12 then %>
noon.
<% end if
if (hour(now) >= 1) and (hour(now) <= 11) then %>
<% =hour(now) %> o'clock AM.
<% end if
if (hour(now) >= 13) and (hour(now) <= 23) then %>
<% =hour(now) - 12 %> o'clock PM.
<% end if %>
If you type (or better yet, cut-and-paste) this script in a Web page, when you view the page, you should see something like this:
The hour is 4 o'clock PM.
If....Then...Else
The If....Then...Else instructions sequence is very similar to the one we may find in different kind of scripting languages. Let's check an example.
<%
AA="water"
If AA="water" Then
response.write ("I want to drink water")
Else
response.write ("I want to drink milk")
End If
%>
<% AA="water"
If AA="water" Then %>
I want to drink water
<% Else %>
I want to drink milk
<% End If %>
In both cases we have checked a condition (AA="water"), and we have a positive instruction (to write the sentence "I want to drink water"). We are allowed to execute any kind of instructions (including If....then....Else) and as many instructions as we want.
For....Next
This instructions is also similar in different programming languages. Let's see a typical example.
example.asp
I want to say "Hello" 10 times<BR>
<% For mynumber = 1 to 10 %>
<% =mynumber %> Hello<BR>
<% Next %>
END
In this case we have defined a variable ("mynumber") and using the For...Next instruction we have repeated 10 times line 4. Similarly to If....Then....Else instruction, we are allowed to execute any kind of instructions and as many of them as we want.
The For...Next instruction allows to define the value of the increment.
<% For mynumber = 1 to 20 STEP 2
response.write("Hello<BR>")
Next %>
<% For mynumber = 20 to 1 STEP -2
response.write("Hello<BR>")
Next %>
In both cases we will get the same response ("Hello" 10 times). The increment may be positive or negative as shown in the example.
Procedures
The ASP source code can contain procedures and functions:
<html>
<head>
<%
sub vbproc(num1,num2)
response.write(num1*num2)
end sub
%>
</head>
<body>
<p>Result: <%call vbproc(3,4)%></p>
</body>
</html>
Next: Differences Between VBScript and JavaScript >>
More ASP Articles
More By Sriram K