Java Reference
In-Depth Information
The solution is rather simple. You create a new Date object based on the year, month, and day
entered by the user. Then you get the day of the week using the Date object's getDay() method. This
returns a number, but by defining an array of days of the week to match this number, you can use
the value of getDay() as the index to your days array.
exercise 2 Question
Create a web page similar to Example 4, but make it display only the hour, minutes, and seconds.
exercise 2 Solution
<!DOCTYPE html>
<html lang="en">
<head>
<title>Chapter 7, Question 2</title>
</head>
<body>
<div id="output"></div>
<script>
function updateTime() {
var date = new Date();
var value = date.getHours() + ":" +
date.getMinutes() + ":" +
date.getSeconds();
document.getElementById("output").innerHTML = value;
}
setInterval(updateTime, 1000);
</script>
</body>
</html>
Save this as ch7 _ question2.html .
Displaying only the hour, minutes, and seconds is an easy task; it just requires a little extra code.
You modify the updateTime() function to first create a Date object to get the time information
from.
var date = new Date();
Then you build a string in hh:mm:ss format:
var value = date.getHours() + ":" +
date.getMinutes() + ":" +
date.getSeconds();
Finally, you output that string to the page:
document.getElementById("output").innerHTML = value;
Search WWH ::




Custom Search