Java Reference
In-Depth Information
For example, if you want to get the month in ourDateObj , you can simply write the following:
theMonth = myDateObject.getMonth();
All the methods work in a very similar way, and all values returned are based on local time,
meaning time local to the machine on which the code is running. It's also possible to use Universal
Time, previously known as GMT, which we discuss in Chapter 7.
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>
 
<html lang="en">
<head>
<title>Chapter 5, Example 6</title>
</head>
<body>
<script>
var months = ["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>
</body>
</html>
Search WWH ::




Custom Search