"Learn ASP programming!" 2-1 Printing variable values |
2-1 Printing variable values
In previous lesson we saw how we can print value of a variable to output page.
If we have a variable "test" we can print its value with two methods.
If we want to print it in html part of our ASP program we will use <%= %> tags.
Example 2-1: 2-1.asp<html>
<head><title>Testing variable output</title></head>
<%
test="Hello"
%>
<br><br>
<p align=center>Value of variable is: <%=test%></p>
</html>If we want to print value of a variable in vbscript section of our ASP program we may use "response" object to do this. In fact we will use one of methods of this object. We will speak more about response.object in next sections.
If look at our 1-5 example in our previous lesson, you will find that this is what we have done in that example.
Example 1-5: 1-5.asp<html>
<head>
<script language="vbscript" runat="server">
sub outnow
response.write("<b>Hello world!</b>")
end sub
</script>
</head>
<body>
I want to say: <br>
<%outnow%>
</body>
</html>
An object is formed of properties (variable members of an object) and methods (function and procedure members of an object). Response object will be used for sending any type of data to web browser.
----------------------------------------------------------2-2 Date and Time variables
Now that we have seen how we can print variables to output, we can work with some of useful internal ASP objects.
First variable that we want to test is "date".
Example 2-2: 2-2.asp
<html>
Today is <%=date%>
</html>Output:
Today is 7/12/2001
You can use "time" variable in the same way you used "date":Example 2-3: 2-3.asp
<html>
Time is <%=time%>
</html>Output:
Time is 7:57:52 PM
If you need both date and time in your output, you can use "now" variable instead.Example 2-4: 2-4.asp
<html>
Now is <%=now%>
</html>Output:
Now is 7/12/2001 7:59:18 PM
----------------------------------------------------------2-3 vbscript functions
Vbscript is a language derived from "Basic" programming language. As you may know there are many instructions or in fact functions in this language that work on variables.
For example, in "Basic" language we have many functions that manipulate a string variable. There are functions that return a part of a string, truncate spaces from its end or start, find a sub string in it, and etc.
We will study most of useful vbscript functions in this course.
In previous section we learned a few of vbscript internal variables. In the next section we are going to see some of the functions related to time and date variables.
----------------------------------------------------------2-4 Time functions
Let's start this section with a comprehensive example.
Look at this example:
Example 2-5: 2-5.asp
<html>
Now is : <%=now%> <br><br>Time: <%=time%><br>
Hour: <%=hour(now)%><br>
Minute: <%=minute(now)%><br>
Second: <%=second(now)%><br><br>Day : <%=day(now) %> <br>
Day : <%=day(date) %> <br>month: <%=month(now)%> <br>
month: <%=month(date)%> <br>Year: <%=year(now) %> <br>
Year: <%=year(date) %> <br><br>
Month Number: <%=month(now)%> <br>
Month Name: <%=monthname(month(now))%> <br>
Month Name: <%=monthname(month(date))%> <br><br>Week Day Number: <%=weekday(now)%><br>
Week Day Name: <%=weekdayname(weekday(now))%><br><br>
</html>
Output:Now is : 7/12/2001 8:11:33 PM
Time: 8:11:33 PM
Hour: 20
Minute: 11
Second: 33Day : 12
Day : 12
month: 7
month: 7
Year: 2001
Year: 2001Month Number: 7
Month Name: July
Month Name: JulyWeek Day Number: 5
Week Day Name: Thursday
If you look at both source code and output you will find what each function does. We have gathered a summary of time functions and variables here.'now' gives current time and date (variable)
'date' gives current date (variable)
'time' gives current clock time. (variable)
'day( )' returns day section of time in number format
'month( )' gives the month section of a date in number format
'year( )' gives the year section of a date
'monthname( )' gives name of a month number in string format i.e. January,
'weekdayname( )' gives name of a week day in string format ie. Sunday,
If you want to use above functions on a given time, you can do this in the same way we have done in below example.
Example 2-6: 2-6.asp<html>
<%
birth="10/12/1971"
%>
My birth day was on <%=birth%><br>
And it was <%=day(birth)%> <%=monthname(month(birth))%>,
<%=year(birth)%>
</html>Output:
My birth day was on 10/12/1971
And it was 12 October, 1971
This is another example that uses a specified time value with above functions.Example 2-7: 2-7.asp
<html>
<%
work="6:04:34"
%>
My work starts at <%=work%><br>
It is at <%=hour(work)%>,<%=month(work)%>,<%=second(work)%>
</html>Output:
My work starts at 6:04:34
It is at 6,12,34----------------------------------------------------------
2-5 Enough
Yes, it's enough for now. In next lesson we will start with some vbscript string functions and then we will proceed to more complicated subjects.
----------------------------------------------------------
Exercises:
Attention:
Do not use any html editing program like MS FrontPage . You must work on the codes yourself.
----------------------------------------------------------
1- Rewrite example 2-5 to use response.write for sending output to web pages. Use example 1-5 as a template.2- Write an ASP program, which has a 'startcounting' sub program. Whenever you call this sub program it writes
the names of weekdays on output web page. Use weekdayname() function.
============================================================