HTML and CSS Reference
In-Depth Information
Table 9-10 toLocaleString() Method
General form:
var variable=dateString.toLocaleString()
Comment:
where dateString is an object instance and the toLocaleString() method converts an object
instance of the Date() to a string using the default display format used on the client computer.
The default display format for the Date() object is Day of the Week Month Date Year HH:MM:SS.
Example:
var curDate=sysDate.toLocaleString()
Result:
curDate contains the date and time stored as: Day of the Week Month Date Year HH:MM:SS
Once the current system date has been converted to a string, the JavaScript
indexOf(), substring(), and substr() methods can be used to extract the day of the week,
the month, date, the year, and the hours (HH), minutes (MM), and seconds (SS) to be
displayed on the Web page.
Using the indexOf() Method The indexOf() method returns the position of the
first occurrence of a specified value in a string. Table 9-11 explains how the indexOf()
method searches a string for a particular value, which is enclosed within the quotation
marks, and then returns the relative location of the value within the string. If the search
finds the value in the search string object, the indexOf() method returns the relative
position of the value within the string object, typically an integer. If the search value is
not found, the indexOf() method returns a negative one (−1). It is important to note that
the first item in a string will be given the value of zero, not one.
Table 9-11 indexOf() Method
General form:
var position=stringValue.indexOf(“x”)
Comment:
where stringValue is a string in which a search is conducted, x is the value to be searched for within
the stringValue, and position is the variable that is assigned the location of x in the string. The value
x must be a literal value.
Examples:
curDate=“February 12, 2014”
dateLocate=curDate.indexOf(“,”)
Using the Current Year
in indexOf()
When extracting
the system date and
comparing it to another
day, the date you search
for in the system Date
in the indexOf() method
must match the current
year, or you will received
incorrect results.
Result:
returns the relative position of the comma found in the string value of curDate: 11
Using the substring() Method to Extract the Month from a String The
substring() method uses two parameters (x,y), where x is the starting point of the string
and y is the location of the last character needed. If only an x parameter is given, the
substring() method returns all the characters starting at that position through the end of
the string. Table 9-12 describes the general form of the substring() method. To extract
the year 2014 from the string using the substring() method, the JavaScript code would be
written as:
birthDay.substring(17,20)
Table 9-12 substring() Method
General form:
var variable=string.substring(x,y)
Comment:
where string is any string object. The substring method extracts a portion of a string, starting
at location x and ending at location y . x and y may be constants or variables.
Example:
weekDay=dayofweek.substring(0, dateLocate)
Result:
the variable weekDay contains the substring result giving the current day of the week
Using the substr() Method The substr() method is similar to the substring()
method, in that it extracts part of a string. Although the methods perform similar
functions in JavaScript, they differ in how they use parameter values. The substring()
method uses the exact byte locations in a string to extract part of the string between the
 
Search WWH ::




Custom Search