HTML and CSS Reference
In-Depth Information
Plan
Ahead
For the dynamic greeting on the home page, you need to calculate the number of days
between two dates. A computer's system date is not stored in a string format that can be
easily used by displaying the contents of the Date() object, so it must be broken apart to
extract the day of the week, date, year, and time. First, you must extract information about
the current date using the following steps:
Obtain the current system date with the Date() object and create a new object
instance by assigning the current date to a variable.
Use the toLocaleString() method to convert the date to a string to be manipulated.
Use the indexOf() method to find the space between the month and the day of the
week to extract the month.
Use the substring() method to extract the day of the week from the string.
Use the substring() method to extract the remainder of the date from the string.
Use the indexOf() method to locate the position of the year in the string.
Use the substring() method to extract the day of the week.
Use the substring() method to extract the current year.
Use the substr() method to extract the year from the string.
Extracting the Current System Date
The built-in Date() object accesses the current system date and time. On the
Midwest Bridal Expo Web page, the Date() object and several of its methods are used
to extract the current system date and then display it on the Web page as part of
the greeting.
To manipulate the Date() object, a new object instance must be created. Table 9-9
shows the general form of the JavaScript statement to create a new object instance, which
uses the new keyword and assigns the built-in object to a variable. This variable is referred
to as an object instance variable .
Date() Object
The Date() object
can use three other
methods to build a
string for a current date:
getDate(), getMonth(),
or getFullYear(). The
getDate() method returns
the date in the month;
getMonth() returns the
value of the month as a
number from 0 to 11 with
0 representing January;
and the getFullYear()
method returns the four
digit year. Because the
getMonth() method
returns an integer that
represents the month,
the developer must add
1 to the result to get the
current month.
Table 9-9 Creating a New Object Instance
General form:
var variableName=new Builtin_Object
Comments:
where variableName is the name of the new object instance, new is the required keyword, and
Builtin_Object is the name of the object from which the new object instance is to be created
Examples:
var sysDate=new Date()
var sysDate=new Date(“February 12, 2014”)
The Date() object also allows developers to enter a specific date and time other than
the current system date and time. The first example shown in Table 9-9 has no specific
date value provided, thus the Date() object accesses the current system date and time from
the computer and stores it in the object instance variable sysDate. In the second example
in Table 9-9, the Date() object has a specific date value enclosed within quotation marks
inside the parentheses and that value (February 12, 2014) is assigned to the object instance
variable sysDate.
Extracting the System
Date
The results in extracting
the date are based upon
the how the system date
is stored. Individual users
and other countries may
use different computer
settings. The default
display is: Day Month Date
Year HH:MM:SS AM/PM.
Converting the System Date to a String To use the date and time value stored in
the variable sysDate, the variable must first be converted to a string, or a series of characters.
To convert the date to a string, you use the toLocaleString() method. Table 9-10 shows the
general form of the toLocaleString() method.
 
Search WWH ::




Custom Search