HTML and CSS Reference

In-Depth Information

Figure 11-22

applying the Math.floor() method

rounds the days value down

to the nearest integer

◗

3.
Save your changes to the file and then reopen or refresh
clock.htm
in your Web

browser. The
daysLeft
field displays the number of days left as an integer. See

Figure 11-23.

Figure 11-23

displaying the integer number of days

integer number of

days left in the year

jbdphotography/Shutterstock.com

Calculating the Hours, Minutes, and Seconds Left in the Year

Next, you want to calculate the hours, minutes, and seconds left in the year. The
Math.

floor()
method determined that the number of whole days left in the year is 310.

The difference between this number and 310.3923... is 0.3923..., which represents the

fractional part of the current day remaining. You need to convert this value to hours. You

can do this by multiplying the fractional part by 24 (the number of hours in a single day)

using the following command:

var hours = (days - Math.floor(days))*24;

For the current date, the value of the
hours
variable is 9.4152 hours or almost 9 and a

half hours. The fractional part of this value represents the minutes and seconds left within

the current hour. As with the
days
variable in the previous set of steps, you need to round