HTML and CSS Reference
In-Depth Information
Plan
Ahead
The steps required to calculate a future date for a dynamic greeting include:
Create a Date() object instance with the future date and the current date.
Use the getTime() method on the current and future date, calculate the milliseconds
between the current date and the future date by subtracting the current getTime()
date from the future getTime() date.
Convert the number of milliseconds to days using the Math.ceil() method to round up
to the next integer.
Calculating the Number of Days Until a Future Event
Calculating the number of days until a future date can be useful for a dynamic
greeting. With the Midwest Bridal Expo Web page, each time users view the Web page in
a browser, the Web page displays a greeting that notifies them of the number of days until
the Midwest Bridal Expo.
Creating a Date() Object Instance to Store a Future Date To calculate the
number of days until a future date, an object instance of the Date() object must be created
using the future date. As previously discussed, the Date() object can have a specific literal
date as a value, which is assigned to an object instance variable. For example, the JavaScript
code to set the date to the Midwest Bridal Expo, which is February 12, 2014, is written as
follows:
var bridalExpo = new Date(“February 12, 2014”)
The object instance variable bridalExpo will now contain the future date of February 12, 2014.
Calculating Milliseconds Between Two Dates Using the getTime()
Method The next step is to calculate the milliseconds between the current date and the
actual date of the Midwest Bridal Expo using the getTime() method of the Date() object.
The getTime() method returns the number of milliseconds that have elapsed since Janu-
ary 1, 1970 at 00:00:00 and another date. Calculating the number of milliseconds between
two dates is easier than trying to count actual days because each month has a different
number of days and it may be necessary to take leap years into account. After determining
the number of milliseconds, you can then convert that value to days.
To determine the number of milliseconds between a current date and another date,
the JavaScript code should be written to subtract the value returned by the getTime()
method of the future date and the value returned by the getTime() method of the current
system date. For example, in this chapter, the JavaScript code is written as follows:
var daysToGo = bridalExpo.getTime()-today.getTime()
where the variable daysToGo will contain the number of milliseconds between the future
date and the current system date.
Converting Milliseconds to Days and Rounding Up Using the ceil()
Method After calculating the number of milliseconds between the current date and
the Midwest Bridal Expo, the next step is to convert the milliseconds to days. To convert
milliseconds to days, the JavaScript code is written to divide the number of milliseconds
stored in the daysToGo variable by the product of 1000*60*60*24. This expression repre-
sents the 1,000 milliseconds in a second, the 60 seconds in a minute, the 60 minutes in an
hour, and the 24 hours in a day.
 
Search WWH ::




Custom Search