HTML and CSS Reference
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
To manipulate the Date() object, a new object instance must be created. Table 9-9
uses the new keyword and assigns the built-in object to a variable. This variable is referred
to as an object instance variable .
The Date() object
can use three other
methods to build a
string for a current date:
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
returns an integer that
represents the month,
the developer must add
1 to the result to get the
Table 9-9 Creating a New Object Instance
var variableName=new Builtin_Object
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
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
Extracting the System
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.