HTML and CSS Reference
In-Depth Information
Table 9.4 Basic Units of Time
Unit of Time
Milliseconds
1 second
1,000
1 minute
second * 60
(1,000 * 60)
1 hour
minute * 60
(1,000 * 60 * 60)
1 day
hour * 24
(1,000 * 60 * 60 * 24 )
1 week
day * 7
(1,000 * 60 * 60 * 24 * 7 )
EXAMPLE 9.18
<html>
<head><title>Countdown 'til Christmas</title></head>
<body bgColor="#00FF99">
<font face="arial" size=5 color=red>
<script type="text/javascript">
1
var today = new Date();
2
var fullyear = today.getFullYear();
3
var future = new Date("December 25, "+ fullyear);
4
var diff = future.getTime() - today.getTime();
// Number of milliseconds
5
var days = Math.floor(diff / (1000 * 60 * 60 * 24 ));
// Convert to days
6
var str="Only <u>" + days + "</u>
shopping days left
'til Christmas! ";
document.write(str+"<br />");
</script>
</font>
</body>
</html>
EXPLANATION
1
A new Date object called today is created.
2
The getFullYear() method returns the year as 2010.
3
Another Date object called future is created. It will contain the future date, Christ-
mas, passed as its argument.
4
The difference between the future time and the present time is calculated and re-
turned in milliseconds with the getTime() method.
5
The Math object is used to round down the result of converting milliseconds to
days.
6
This string contains the number of days between the present date and Christmas
(see Figure 9.22).
 
Search WWH ::




Custom Search