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
<head><title>Testing variable output</title></head>
<p align=center>Value of variable is: <%=test%></p>
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
<script language="vbscript" runat="server">
I want to say: <br>
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.
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
Today is <%=date%>
Today is 7/12/2001
You can use "time" variable in the same way you used "date":
Example 2-3: 2-3.asp
Time is <%=time%>
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
Now is <%=now%>
Now is 7/12/2001 7:59:18 PM
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 next section we are going to see some of
functions related to time and date variables.
Let's start this section with a comprehensive example.
Look at this example:
Example 2-5: 2-5.asp
Now is : <%=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>
Now is : 7/12/2001 8:11:33 PM
Time: 8:11:33 PM
Day : 12
Day : 12
Month Number: 7
Month Name: July
Month Name: July
Week 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
My birth day was on <%=birth%><br>
And it was <%=day(birth)%> <%=monthname(month(birth))%>,
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
My work starts at <%=work%><br>
It is at <%=hour(work)%>,<%=month(work)%>,<%=second(work)%>
My work starts at 6:04:34
It is at 6,12,34
In next lesson we will start with some vbscript string functions and then we will proceed to more complicated subjects.
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.