Java Reference
In-Depth Information
❑
getSeconds()
❑
getMilliseconds()
❑
toTimeString()
These methods return respectively the hours, minutes, seconds, milliseconds, and full time of the
specifi ed
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 PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<title>Chapter 5: Example 7</title>
</head>
<body>
<script type=”text/javascript”>
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”;
}