Windows NT Internet and Intranet Development

To learn more about author Sanjaya Hettihewa, please visit the author's homepage.

Previous chapterNext chapterContents


- 18 -

Time- and String-Manipulation Functions


VBScript time- and string-manipulation functions are useful when developing Web applications. This chapter comprehensively covers these functions and demonstrates how they are used in VBScript applications.

Time-Manipulation Functions

Web applications can be refined and given a professional touch by using time-manipulation functions of the VBScript language. For example, depending on the time of day, a VBScript application can greet the user (good morning, good evening, and so on) and provide time-sensitive information (stock prices, current weather, and so on). Listed next are some of the tasks that can be performed using VBScript time-manipulation functions.

Listing 18.1. Invalid date-detection subroutine.

1: If (IsDate (UserInput) = FALSE) Then
2:   MsgBox "Invalid Input Detected." & Chr(10) & _
3:          "I'm sorry but the date you entered is " & Chr(10) &  _
4:          "not a valid day. Please enter a valid " & Chr(10) _
5:          & "date and try again." _
6:          , 4112, "Invalid Input Detected"
7: End If 


Figure 18.1. Error message generated by lines 2-6 of Listing 18.1.

Listing 18.2. The time-sensitive version of the Hello World! application.

 1: <!--
 2:   © 1996 Sanjaya Hettihewa (http://www.NetInnovation.com/)
 3:   All Rights Reserved.
 4:   Permission is hereby given to modify and distribute this code as you wish
 5:   provided that this block of text remains unchanged.
 6:  !-->
 7:
 8: <HTML>
 9:
10: <HEAD>
11: <TITLE>Hello World! (time sensitive version)</TITLE>
12: </HEAD>
13:
14: <BODY BGCOLOR=FFFFCC>
15:
16: <H1>Hello World! (time sensitive version)</H1>
17:
18: <TABLE BORDER=4 BGCOLOR="000000">
19: <TR><TD>
20: <FONT COLOR="FFFFFF">
21:     <SCRIPT LANGUAGE="VBScript">
22: <!--
23:
24:   CurrentHour = Hour(Time)
25:
26:   document.Write "<H3>Current time: " & Time & "<BR>"
27:   document.Write "Current date: " & Date & "</H3>"
28:
29:   If (CurrentHour < 6 ) Then
30:     document.Write _
31:     "<H2>Hi! What are you doing up in the wee hours of the morning?</H2>"
32:   Else If (CurrentHour < 12 ) Then
33:     document.Write _
34:     "<H2>Hi! Good morning!</H2>"
35:   Else If (CurrentHour < 14) Then
36:     document.Write _
37:     "<H2>Hi! Good afternoon!</H2>"
38:   Else
39:     document.Write _
40:     "<H2>Hi! Good evening!</H2>"
41:   End If
42:   End If
43:   End If
44:
45: -->
46:     </SCRIPT>
47: </FONT>
48: </TD></TR>
49: </TABLE>
50:
51: </BODY>
52: </HTML> 


Figure 18.2. Output of the VBScript application in Listing 18.2.

The syntax and usage information of VBScript time-manipulation functions are given next.

CDate()

If a valid date expression is passed into the function, it is converted into date type and returned. Before passing an expression to the CDate() function, it is possible to determine whether it can be converted by CDate() into date type by using the IsDate() function. Always use the IsDate() function to validate dates entered by users. Do not assume dates typed by users are valid.

Date()

Date() returns the date from the system clock.

DateSerial()

DateSerial() is a handy function that can be used to calculate various days. By using numeric expressions, it is possible to use the DateSerial() function to count back and forward from a date simply by adding and subtracting numbers. The syntax of the DateSerial() function is as follows:

DateSerial (<Year>, <Month>, <Day>)

For example, if the current date is 4/1/1996, DateSerial(1996,4-2,1+28) returns the value 2/29/1996. Of course, if the year was 1997 (not a leap year), the result would have been 3/1/1997.

DateValue()

DateValue() converts an expression passed to the function into a variant of subtype date and returns it. For example, DateValue("February 29, 1976") returns 2/29/1976. If the year is left out, it is obtained from the system clock.

Day()

The Day() function returns a value between 1 and 31 and can be used to find the day of a date. For example, Day("4/1/1996") returns 1.

Hour()

Hour() returns the number of hours of a time expression. For example, Hour("12:25:34") returns 12.

IsDate()

IsDate() returns TRUE if an expression can be converted to a valid date and FALSE otherwise.


Note: When accepting a date from a user, always use the IsDate() function to make sure the date entered by the user is valid.

Minute()

Minute() returns the number of minutes when called with the time. For example, Minute("23:50:45") returns 50.

Month()

Month() returns the month when called with a date. For example, Month("4/1/1996") returns 4.

Now()

Now() returns the current date and time, respectively, from the system clock. For example, the Now() command returned the string 4/1/1996 23:08:31 at the time of this writing.

Second()

Second() returns the number of seconds of a date expression. For example, Second("18:23:57") returns 57.

Time()

Time() returns the current time from the system clock. For example, the value 01:23:48 was returned by the Time() function at the time of this writing.

TimeSerial()

TimeSerial() is a very handy function that can be used to perform time calculations. For example, if the current time is 12:30, TimeSerial() can be used to calculate the time 25 minutes ago. In this case, TimeSerial(12,30-25, 0) returns 12:05:00.

TimeValue()

TimeValue() returns an expression passed to the function after converting it into a variant of subtype Date. For example, TimeValue ("2:35:17pm") returns 14:35:17.

Weekday()

The Weekday() function returns a number between 1 and 7. The numbers returned by the Weekday() function correspond to days of the week, as shown in Table 18.1.

Table 18.1. Day codes.

Day code Day of week
1 Sunday
2 Monday
3 Tuesday
4 Wednesday
5 Thursday
6 Friday
7 Saturday
For example, Weekday("April 2, 1996") returns 3--which is, indeed, a Tuesday.

Year()

Year() returns the year of the expression. For example, Year("February 29, 1976") returns 1976.

String-Manipulation Functions

String-manipulation functions are generally used to process data entered by users. The following VBScript functions can be used to manipulate string expressions.

LSet

LSet is used to copy a variable of one user-defined type to a variable of another user- defined type. When a variable is copied with the LSet command, it is left aligned. The syntax of the LSet statement is as follows:

LSet <Variable> = <ValueOfVariable>

If the length of <Variable> is longer than that of <ValueOfVariable> after you copy <ValueOfVariable> to <Variable>, the remaining space is filled in with white spaces. Likewise, if the length of <Variable> is less than that of <ValueOfVariable>, <ValueOfVariable> is truncated to fit in the space allocated for <Variable>. For example, if <Variable> can hold only four characters and <ValueOfVariable> contains the string "ABCDEFG", <Variable> will have the value "ABCD" after <ValueOfVariable> is copied to <Variable> using the LSet command.

Mid

Mid is a handy statement for replacing one or more characters of a string with characters from another string. The syntax of the Mid statement is as follows:

Mid (<Variable>, <Begin>, <NumCharactersToReplace>) = <Replacement>

The Mid statement is used by replacing placeholders of the preceding example as follows:

RSet

The RSet command is similar in functionality to the LSet command. The only difference is that when a variable is assigned a string using the RSet command, it is assigned to the variable right aligned. The syntax of the RSet command is as follows:

RSet <Variable> = <StringToCopy>

CStr()

CStr() converts an expression passed to CStr into a string and returns it. For example, CStr(123.456) returns the value "123.456".

InStr()

InStr() returns the location of one string in another string. The syntax of InStr() is as follows:

InStr (<BeginPosition>, <String1>, <String2>, <ComparisonType>

The InStr() function is used by replacing italicized placeholders as follows:

The InStr() function can be used to validate an e-mail address entered by a user. By itself, VBScript cannot contact a remote mail server and verify an e-mail address provided by a user; however, the e-mail address can be examined with the aid of VBScript string-manipulation functions. The application in Listing 18.3 demonstrates how the InStr() function is used to validate an e-mail address by verifying that it conforms to the format <text>@<text>.<text>. If an invalid e-mail address is entered, the e-mail address validation application displays a dialog box (as shown in Figure 18.3). The dialog box in Figure 18.4 is displayed if the e-mail address conforms to the format <text>@<text>.<text>.

Listing 18.3. The e-mail address validation application.

 1: <!--
 2:   © 1996 Sanjaya Hettihewa (http://www.NetInnovation.com/)
 3:   All Rights Reserved.
 4:   Permission is hereby given to modify and distribute this code as
 5:   you wish provided that this block of text remains unchanged.
 6:  !-->
 7: <HTML>
 8: <HEAD>
 9: <TITLE>Validating An E-Mail Address</TITLE>
10: </HEAD>
11: <BODY BGCOLOR=FFFFCC>
12: <H1>Validating An E-Mail Address</H1>
13: <TABLE BORDER=4 BGCOLOR="000000">
14: <TR><TD>
15: <FONT COLOR="FFFFFF">
16: <form name="EMailAddressEntryForm">
17: <p><H3>Please enter an e-mail address to validate</H3>
18: <input type="text" size="40" name="EMailAddress">
19: <input type="button" name="Validate" value="Validate E-Mail Address"></p>
20: </form>
21: </FONT>
22: </TD></TR>
23: </TABLE>
24:     <SCRIPT LANGUAGE="VBScript">
25: <!--
26: Sub Validate_OnClick
27:   EMailAdress = EMailAddressEntryForm.EMailAddress.Value
28:   AtSignPosition =  InStr (EMailAdress,"@")
29:   If (AtSignPosition > 0) Then
30:     PeriodPosition =  InStr (AtSignPosition,EMailAdress,".")
31:   Else
32:     PeriodPosition =  0
33:   End If
34:   AddressLength = Len(EMailAdress)
35:   If (AtSignPosition = 0) Then
36:     MsgBox "Invalid e-mail address entered!"
37:   Else If (PeriodPosition = 0) Then
38:     MsgBox "Invalid e-mail address entered!"
39:   Else If (AddressLength=PeriodPosition) Then
40:     MsgBox "Invalid e-mail address entered!"
41:   Else
42:     MsgBox "Valid e-mail address entered!"
43:   End If
44:   End If
45:   End If
46: End Sub
47: -->
48:     </SCRIPT>
49: </BODY>
50: </HTML>

Figure 18.3. Output of the e-mail address validation application when an invalid e-mail address is entered.



Warning: The application in Listing 18.3 is presented to give you an overview of how the InStr() function is used to validate data entered by the user--not to demonstrate how to extensively validate an e-mail address. Additional data validation may be required to validate an e-mail address.

Figure 18.4. Output of the e-mail address validation application when a valid e-mail address is entered.


LCase()

LCase() converts a string expression to lowercase and returns it.

Left()

Left() returns a certain number of characters from the left side of a string. For example, Left("Windows NT", 7) returns "Windows".

Len()

Len() returns the number of characters of a string expression.

LTrim, RTrim, and Trim

LTrim, RTrim, and Trim eliminate spaces from a string and return it. LTrim eliminates preceding spaces, RTrim eliminates trailing spaces, and Trim eliminates both trailing and preceding spaces.

Mid()

Mid() returns a certain number of characters from a string. For example Mid("Windows NT", 0, 7) returns "Windows".

Right()

Right() returns a certain number of characters from the right side of a string. For example, Right("Windows NT", 2) returns "NT".

Str()

Str() converts a numeric expression into a string and returns it. The Str() function returns a string representation of a number, while the CStr() function converts an expression to a string.

StrComp()

The StrComp() function is used to compare strings. The syntax of the StrComp() function is as follows:

StrComp (<String1>, <String2>, <ComparisonMethod>)

After StrComp() compares both strings, it returns 0 if both strings are identical, -1 if <String1> is less than <String2>, and 1 otherwise. The <ComparisonMethod> argument is optional. If it is 0, a binary comparison is performed, and if it is 1, a case-insensitive comparison is performed. If <ComparisonMethod> is left out, a binary comparison is performed.

String()

The String() function is handy for repeating a character a certain number of times. For example String(5,"*") can be used to create a string containing five asterisks.

UCase()

UCase() converts strings passed to the function into uppercase and returns them. For example, UCase("Windows NT") returns "WINDOWS NT".

Val()

The Val() function can be used to obtain a number contained in a string. The function scans the string until it encounters a character that is not part of a number. For example, Val(" 1234 567 in a string") returns the number 1234567.

Summary

Virtually all applications manipulate time and string expressions. Due to the text-centric nature of the World Wide Web, numerous time and string expressions are often processed by Web applications. The syntax of VBScript time- and string-manipulation functions is discussed in this chapter to provide you with an overview of how they can be used in VBScript Web applications.


Previous chapterNext chapterContents


© Copyright, Macmillan Computer Publishing. All rights reserved.