



(6 ratings)
We'll continue with some additional examples of VBScript Date() and Time() functions to help elaborate the ideas behind server-side scripting. In the following example we use the functions:
| Function | Returned Value |
| Date() | Returns the system date in the format mm/dd/yy. |
| Time() | Returns the system time in the format hh:mm:ss AM|PM. |
| Now() | Returns the system date and time in the format mm/dd/yy hh:mm:ss AM|PM. |
| Hour(Time()) | Returns the hour of the day in 24-hour (military) time. |
| Year(Date()) | Returns the year portion of the system date in the format 9999. |
| Month(Date()) | Returns the numeric month of the year (January = 1). |
| MonthName(Month(Date())) | Returns the name of the month. |
| Day(Date()) | Returns the numeric day of the month. |
| WeekDay(Date()) | Returns the numeric day of the week (Sunday = 1). |
| WeekDayName(WeekDay(Date())) | Returns the name of the day of the week. |
| DateAdd() | Adds date and time intervals to derive a new date or time. |
| DateDiff() | Returns the difference between dates and times. |
| FormatDateTime() | Formats display of dates and times. |
Note that several of the returned values are given by functions applied to other functions. For instance, MonthName(Month(Date())) returns the name of the month. To derive this value, the Month() function is first applied to the Date() function to extract the numeric month of the year (January = 1) from the system date. Then the MonthName() function is applied to this numeric result to get the name of the month.
We'll put these and other functions together inside a script that appears next on this page to produce the following output:
Today's date is Wednesday, June 25, 2008
<%
TheDayName = WeekDayName(WeekDay(Date()))
TheMonthName = MonthName(Month(Date()))
TheDay = Day(Date())
TheYear = Year(Date())
Response.Write("Today's date is " & TheDayName & ", " & TheMonthName & " " &
TheDay & ", " & TheYear)
%>
</span>
The HTML code on this page includes a <span> tag to render the enclosed script's output in the displayed font size and color. The script begins by assigning function results to script variables for ease of reference. The variable TheDayName is assigned the result of applying the functions WeekDayName(Weekday(Date())). Thus, the value Wednesday is stored in this variable. Similarly, The variables TheMonthName, TheDay, and TheYear are assigned their respective values ( June, 25, and 2008) from associated functions.
Once these server values are stored as script variables, they are written to the page with a Response.Write() statement. Text strings and server values are concatenated to produce the output string displayed on the page.
As a reminder that server values can be embedded directly within HTML by placing them between <%= and %> symbols, the following code can be used to produce the same results:
TheDayName = WeekDayName(WeekDay(Date()))
TheMonthName = MonthName(Month(Date()))
TheDay = Day(Date())
TheYear = Year(Date())
%>
<span style="color:#0000FF; font-size:12pt; font-weight:bold">
Today's date is <%=TheDayName%>, <%=TheMonthName%> <%=TheDay%>, <%=TheYear%>
</span>
In fact, it is not necessary to store function results as variables prior to displaying them. The above script can be eliminated and the HTML modified to include the embedded functions themselves:
Today's date is <%=WeekDayName(WeekDay(Date()))%>, <%=MonthName(Month(Date()))%><%=Day(Date())%>, <%=Year(Date())%>
</span>
Script Decision Making
In addition to the date display let's add a greeting whose format depends on the time of day. If the current time is prior to 12:00 noon, then the greeting is "Good Morning." If the time is between 12:00 noon and 6:00 PM, then the greeting is "Good Afternoon." If the time is after 6:00 in the evening, then the greeting is "Good Evening."
Good Morning. Today's date is Wednesday, June 25, 2008.
If Hour(Time) < 12 Then
Greeting = "Good Morning"
ElseIf Hour(Time) < 18 Then
Greeting = "Good Afternoon"
Else
Greeting = "Good Evening"
End If
%>
<span style="color:#0000FF; font-size:12pt; font-weight:bold">
<%=Greeting%>. Today's date is <%=WeekDayName(WeekDay(Date()))%>,
<%=MonthName(Month(Date()))%> <%=Day(Date())%>, <%=Year(Date())%>.
</span>
We can specify that a particular greeting be displayed by coding a script to check the hour of the day and then format the appropriate greeting. To do so, we need to use the VBScript If...End IfIf...Else...End If decision-making structure. Just as in Visual Basic, VBScript contains an structure. Its general format is
|
If condition test Then ...statements [ ElseIf condition test Then ...statements ] ... End If |
with condition tests formed using the conditional operators
| < | less than |
| = | equal to |
| > | greater than |
| <= | less than or equal to |
| >= | greater than or equal to |
| <> | not equal to |
and with compound condition tests constructed with the logical operators
ANDOR
NOT
Our script contains condition tests to determine within which of the three time ranges the current hour of the day falls. In order to determine the current hour of the day, we use the Hour(Time())Time() function returns the current system time in the format hh:mm:ss; the Hour() functions. The function applied to this value extracts the hour portion, converting it to military (24-hour) time.
If Hour(Time) < 12 Then
Greeting = "Good Morning"
ElseIf Hour(Time) < 18 Then
Greeting = "Good Afternoon"
Else
Greeting = "Good Evening"
End If
%>
First, we check whether the current hour is less than 12 (before noon). If so, then we store the character string "Good Morning" in the variable named Greeting. If this condition test is false, we proceed to check whether the current hour is less than 18 (6:00 in the evening). If so, then we store the character string "Good Afternoon" in the variable named Greeting. Otherwise, it's later than 6:00 in the evening and we store the character string "Good Evening" in the variable. At the completion of the script, one of the three text strings is stored in the variable depending on the hour of the day.
Script output is embedded within the HTML on the page to display the greeting along with the date:
<%=Greeting%>. Today's date is <%=WeekDayName(WeekDay(Date()))%>,
<%=MonthName(Month(Date()))%> <%=Day(Date())%>, <%=Year(Date())%>.
</span>
Remember that the server runs the script prior to sending the page to the browser. Therefore, all that is returned to the browser is the results of script processing, not the script itself. If you were to take a look at your browser's source listing for this page, all you would see are the lines:
<span style="color:#0000FF; font-size:12pt; font-weight:bold">
Good Morning. Today's date is Wednesday, June
25, 2008.
</span>
Comparing Dates and Times
Intervals between dates and times are derived from the functions DateDiff() and DateAdd(). Both functions use the following interval strings to indicate the unit of measurement.
| Interval | Description |
| yyyy | Year |
| q | Quarter |
| m | Month |
| y | Day of year |
| d | Day |
| w | Weekday |
| ww | Week of year |
| h | Hour |
| n | Minute |
| s | Second |
The DateDiff() function requires three arguments:
|
DateDiff(interval, first date, second date) |
where interval is the unit of measurement as an interval string and first date and second date are literal dates or the system date. For example, the following function returns the number of days until Christmas:
The "d" parameter specifies the measurement in days, the Date() function retrieves the current date, and "12/25/2001" is the comparison date.
The DateAdd() function also requires three arguments:
|
DateAdd(interval, multiplier, date) |
where interval is the unit of measurement as an interval string, multiplier is the number of units to add, and date is a literal date or the system date. For example, the following function returns the time 2 hours from now:
The "h" parameter specifies the measurement in hours, the 2 specifies the addition 2 hours, and Time is the current system time.
Formatting Dates and Times
By default, dates are displayed in short date format (6/25/2008) and times are displayed in long time format (10:23:58 AM). These displays can be formatted by using the FormatDateTime() function to specify either a short or long date or time.
The long date is <%=FormatDateTime(Date(),1)%>.
The short time is <%=FormatDateTime(Time(),4)%>.
The long time is <%=FormatDateTime(Time(),0)%>.
The long date is Wednesday, June 25, 2008.
The short time is 10:23.
The long time is 10:23:58 AM.
By passing the date parameter 0 (short) or 1 (long), the function returns the appropriately formatted date. By passing the time parameter 4 (short) or 0 (long), the function returns the appropriately formatted time.
20 Random Tutorials from the same category :
DISPLAY DATA FROM DATABASE
ASP.NET in Dreamweaver 8
Counter in ASP
Processing Form Information In ASP
Date and Time Functions In ASP
FORMAT SPECIAL DATA
Installing ASP on your own computer
The Recordset Object
SQL Access Methods In ASP
Server-side Scripting
Server Variables In ASP
Creating Databases
ASP.NET 2 Special Purpose Folders
The Connection Object
Searching for Matching Values
Passing Query Strings In Asp
Recordset Access Methods
Text Editors and Software to Write ASP
Randomizer for ASP
Reading form variables passed in the URL













