HTML and CSS Reference
In-Depth Information
Figure 11-25
displaying the integer number of hours left in the day
integer number of
hours left in the day
jbdphotography/Shutterstock.com
The technique to calculate the minutes left in the current hour is similar to the one
you used to calculate the hours left in the current day. You multiply the difference
between the
hours
value and the whole hours value by 60 (the number of minutes in
an hour) to express the fractional part in terms of minutes, as shown in the following
command:
var minutes = (hours - Math.floor(hours))*60;
Finally, to calculate the seconds left in the current minute, you multiply the fractional
part of the
minutes
variable by 60 (the number of seconds in a minute), as follows:
var seconds = (minutes - Math.floor(minutes))*60;
As with the
days
and
hours
variables, you want to display only the integer part of the
minutes
and
seconds
variables by using the
Math.floor()
method. You'll add these
commands to the
NYClock()
function.
To calculate the minutes and seconds left:
◗
1.
Return to the
clock.htm
file and the NYClock() function in your text editor.
◗
2.
Below the statement that sets the value of the
hrsLeft
field, insert the following lines:
// calculate the minutes left in the current hour
var minutes = (hours - Math.floor(hours))*60;
◗
3.
Change the statement that sets the value of the
minsLeft
field to:
// display minutes rounded to the next lowest integer
document.clockform.minsLeft.value = Math.floor(minutes) + “
minute(s)”;
◗
4.
Below the statement that sets the value of the
minsLeft
field, insert the
following lines:
// calculate the seconds left in the current minute
var seconds = (minutes - Math.floor(minutes))*60;
◗
5.
Change the statement that sets the value of the
secsLeft
field to:
// display seconds rounded to the next lowest integer
document.clockform.secsLeft.value = Math.floor(seconds) + “
second(s)”;