HTML and CSS Reference
In-Depth Information
time value stored in the today variable to a string, using the toLocaleString() method
and assigns it to the dayofweek variable. To find the day of the week in the string, the
indexOf() method in line 11 looks for the first blank space in the string. Line 12 uses the
substring() method to extract the day of the week, while line 13 extracts the remainder
of the string, which includes the month, day, year, and time. (Remember that when the
substring() method is used with one variable, the data extraction starts at the position of
the given variable and goes all the way through the end of the string.) Line 14 looks for
the comma that separates the date from the year using the indexOf() method. Using the
address of the comma, the substring() method in line 15 extracts the date. To find the year,
line 16 uses the indexOf() method and the current year to determine the starting address
of the year. When inserting the date at line 16, you must use the current year. Line 17 uses
the substr() method, using the starting address from the indexOf() method and length of
the year, which is four characters long.
To Extract the Current System Date Using the Date() Object
This step writes JavaScript code that uses the Date() object and its methods to extract the current system date.
1
If necessary, activate the Notepad++ window.
If necessary, click line 8
below the <!--Hide
from old browsers
statement.
Enter the JavaScript
code shown in
Table 9-15, indenting
as shown. Press e n t e r
at the end of each
complete line of code.
If the current year is
different from 2014,
replace 2014 with the
current year in the
indexOf() method on
line 16.
Statements to extract current system date
object instance variable “today” will be
used to calculate number of days from
current date until specified future date
insertion point
current year entered
After typing the last
line in Table 9-15, press
the e n t e r key twice to
leave space for addi-
tional JavaScript code.
Compare what you
typed to Figure 9-5.
Correct any errors
before proceeding.
Figure 9-5
Why is some of the code in Figure 9-5 indented?
The code is indented with the s p a c e b a r for ease of reading. It does not affect the execution of
the code. You may want to indent sections of code to make it easier to identify.
What happens if a different year is entered in the indexOf() method than what is in the system?
The year will never match and JavaScript returns unusual looking results, but not as an error message.
 
Search WWH ::




Custom Search