Java Reference
In-Depth Information
All the methods work in a very similar way, and all values returned are based on local time, meaning
time local to the machine the code is running on. It's also possible to use Universal Time, previously
known as GMT, which we'll discuss in Chapter 9.
Try It Out Using the Date Object to Retrieve the Current Date
In this example, you use the get date type methods you have been looking at to write the current day,
month, and year to a web 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 6</title>
</head>
<body>
<script type=”text/javascript”>
var months = new Array(“January”,”February”,”March”,”April”,”May”,”June”,”July”,
“August”, “September”, “October”, “November”, “December”);
var dateNow = new Date();
var yearNow = dateNow.getFullYear();
var monthNow = months[dateNow.getMonth()];
var dayNow = dateNow.getDate();
var daySuffix;
switch (dayNow)
{
case 1:
case 21:
case 31:
daySuffix = “st”;
break;
case 2:
case 22:
daySuffix = “nd”;
break;
case 3:
case 23:
daySuffix = “rd”;
break;
default:
daySuffix = “th”;
break;
}
document.write(“It is the “ + dayNow + daySuffix + “ day “);
document.write(“in the month of “ + monthNow);
document.write(“ in the year “ + yearNow);
</script>
Search WWH ::




Custom Search