Java Reference
In-Depth Information
➤
getSeconds()
➤
getMilliseconds()
➤
toTimeString()
These methods return, respectively, the hours, minutes, seconds, milliseconds, and full time of the
specified
Date
object, where the time is based on the 24‐hour clock:
0
for midnight and
23
for 11
p.m. The last method is similar to the
toDateString()
method in that it returns an easily readable
string, except that in this case it contains the time (for example,
"13:03:51 UTC"
).
trY it out
Writing the Current time into a Web page
Let's look at an example that writes out the current time to the page:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Chapter 5, Example 7</title>
</head>
<body>
<script>
var greeting;
var nowDate = new Date();
var nowHour = nowDate.getHours();
var nowMinute = nowDate.getMinutes();
var nowSecond = nowDate.getSeconds();
if (nowMinute < 10) {
nowMinute = "0" + nowMinute;
}
if (nowSecond < 10) {
nowSecond = "0" + nowSecond;
}
if (nowHour < 12) {
greeting = "Good Morning";
} else if (nowHour < 17) {
greeting = "Good Afternoon";
} else {
greeting = "Good Evening";
}
document.write("<h4>" + greeting + " and welcome to my website</h4>");
document.write("According to your clock the time is ");
document.write(nowHour + ":" + nowMinute + ":" + nowSecond);
</script>
</body>
</html>