HTML and CSS Reference

In-Depth Information

The value returned from the calculation daysToGo/(1000*60*60*24) will probably

contain a decimal value. The Midwest Bridal Expo Web page, however, should display

the number of days to the Midwest Bridal Expo as an integer value. The ceil() method

of the Math object is used to round up to the nearest integer (for example, if the result is

12.843 days, 13 will display because of the ceil() method). The general form of the ceil()

method is shown in Table 9-16.

Table 9-16 Math ceil() Method

General form:

var variable=ceil(value)

Comment:

where
value
may be the result of any calculation. The
ceil()
method returns a value that rounds

the value up to the next highest integer.

Examples:

var myResult=ceil(−3.8)

var myNumber=ceil(4.179)

Result:

myResult is −3

myNumber is 5

The JavaScript code for the Midwest Bridal Expo Web page is written as follows:

var daysToBridalExpo = Math.ceil(daysToGo/(1000*60*60*24))

which first finds the product of 1000*60*60*24, then divides the value stored in the

daysToGo variable by this product, and then raises the result (rounds up) to the next

highest integer.

To Calculate the Number of Days to a Future Date

The code to calculate the number of days to a future date is shown in Table 9-17. Line 19 creates the

bridalExpo object from the future date of the Midwest Bridal Expo in 2014. Line 20 subtracts the number of

milliseconds of the current date (today.getTime()) from the future date (bridalExpo.getTime()). Line 21 converts

the number of milliseconds to days.

Table 9-17 Code to Calculate the Number of Days to the Midwest Bridal Expo

Line

Code

19

var bridalExpo = new Date(“February 12, 2014”)

20

var daysToGo = bridalExpo.getTime()-today.getTime()

21

var daysToBridalExpo = Math.ceil(daysToGo/(1000*60*60*24))

The step on the next page enters the code to calculate the number of days to the Midwest Bridal Expo.