This article gives examples of how use Active Server Pages (ASPs) to customize the display of the current date and time on your Web page. There are also examples of how to display text such as assessing the length of a line (how many characters it has), instructions such as If...Then...Else, and how to process information a user has just typed into a form.
Contributed by Sriram K Rating: / 230 May 24, 2004
SEARCH ASP FREE
See Also:
TOOLS YOU CAN USE
advertisement
Active Server Pages (ASPs) are Web pages that contain server-side scripts in addition to the usual mixture of text and HTML (Hypertext Markup Language) tags. Server-side scripts are special commands you put in Web pages that are processed before the pages are sent from your Personal Web Server to the Web browser of someone who's visiting your Web site. When you type a URL in the Address box or click a link on a Web page, you're asking a Web server on a computer somewhere to send a file to the Web browser (sometimes called a "client") on your computer. If that file is a normal HTML file, it looks exactly the same when your Web browser receives it as it did before the Web server sent it. After receiving the file, your Web browser displays its contents as a combination of text, images, and sounds.
In the case of an Active Server Page, the process is similar, except there's an extra processing step that takes place just before the Web server sends the file. Before the Web server sends the Active Server Page to the Web browser, it runs all server-side scripts contained in the page. Some of these scripts display the current date, time, and other information. Others process information the user has just typed into a form, such as a page in the Web site's guestbook.
To distinguish them from normal HTML pages, Active Server Pages are given the ".asp" extension.
What Can You Do with Active Server Pages?
There are many things you can do with Active Server Pages. You can display date, time, and other information in different ways. You can make a survey form and ask people who visit your site to fill it out, send emails, save the information to a file, etc.
What Do Active Server Pages Look Like?
The appearance of an Active Server Page depends on who or what is viewing it. To the Web browser that receives it, an Active Server Page looks just like a normal HTML page. If a visitor to your Web site views the source code of an Active Server Page, that's what they see: a normal HTML page. However, the file located in the server looks very different. In addition to text and HTML tags, you also see server-side scripts. This is what the Active Server Page looks like to the Web server before it is processed and sent in response to a request.
What Do Server-Side Scripts Look Like?
Server-side scripts look a lot like HTML tags. However, instead of starting and ending with lesser-than ( < ) and greater-than ( > ) brackets, they typically start with <% and end with %>. The <% is called an opening tag, and the %> is called a closing tag. In between these tags are the server-side scripts. You can insert server-side scripts anywhere in your Web page -- even inside HTML tags.
Do You Have to Be a Programmer to Understand Server-Side Scripting?
There's a lot you can do with server-side scripts without learning how to program. For this reason, much of the online Help for Active Server Pages is written for people who are familiar with HTML but aren't computer programmers.
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.
Suppose 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:
When you view 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 Weekdayname
To 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 Second
To 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.
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.
Timevalue
You 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.
Example 3
Earlier 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.
DisplayingText
len
The len function tells you how many characters are in a word or sequence of words. (The name "len" is an abbreviation of "length".) All characters are counted, including the space character. For example, to find the length of the sentence "The cat is on the mat," type this into a Web page:
There are <% =len("The cat is on the mat.") %> characters in "The cat is on the mat."
When you view the page in Internet Explorer, you should see this:
There are 22 characters in "The cat is on the mat."
left
You can use the left function to look at the first few characters of a word or sequence of words. For example, to find the first character of "Frankenstein," type this into a Web page:
"Frankenstein" begins with the letter <% =left("Frankenstein", 1) %>.
When you view the page, you should see this:
"Frankenstein" begins with the letter F.
right
To look at the last few characters of a word or sequence of words, use the right function. For example, to find the last three letters of "Wednesday," type this into a Web page:
The last three letters of "Wednesday" are: <% =right("Wednesday", 3) %>.
When you view this page, you should see this:
The last three letters of "Wednesday" are: day.
Example 4
What if you wanted to take a few letters from the middle of something? How would you specify exactly where in the middle you wanted to be? For example, how would you take out just the "apple" part of the word "pineapples"?
You could start with the fifth character from the left and then stop at the second character from the right. Or you could do it the following way.
This line takes the last six letters of the word "pineapples," which make up the word "apples." Then it takes the first five letters of the word "apples," which make up the word "apple." When you view this page in Internet Explorer, you should see this:
This line takes the first nine letters of the word "pineapples," which make up the word "pineapple." Then it takes the last five letters of the word "pineapple," which make up the word "apple."
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:
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>
When calling a VBScript or a JavaScript procedure from an ASP file written in VBScript, you can use the "call" keyword followed by the procedure name. If a procedure requires parameters, the parameter list must be enclosed in parentheses when using the "call" keyword. If you omit the "call" keyword, the parameter list must not be enclosed in parentheses. If the procedure has no parameters, the parentheses are optional.
When calling a JavaScript or a VBScript procedure from an ASP file written in JavaScript, always use parentheses after the procedure name.
Request.QueryString
The Request.QueryString command is used to collect values in a form with method="get". Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser's address bar) and has limits on the amount of information to send.
If a user typed "Bill" and "Gates" in the form example above, the URL sent to the server would look like this:
Assume that the ASP file "simpleform.asp" contains the following script:
The browser will display the following in the body of the document:
Welcome Bill Gates
Request.Form
The Request.Form command is used to collect values in a form with method="post". Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.
If a user typed "Bill" and "Gates" in the form example above, the URL sent to the server would look like this:
Assume that the ASP file "simpleform.asp" contains the following script:
The browser will display the following in the body of the document:
Welcome Bill Gates
Form Validation
User input should be validated on the browser whenever possible (by client scripts). Browser validation is faster and you reduce the server load.
You should consider using server validation if the user input will be inserted into a database. A good way to validate a form on the server is to post the form to itself, instead of jumping to a different page. The user will then get the error messages on the same page as the form. This makes it easier to discover the error.