Using the methods in the previous list is simple and straightforward as Listing A illustrates.
One thing you will notice is the month and weekday values begin with zero, so you'll need to add one to them to display their actual values. You could easily use an array to display the actual day of the week's name.
Listing B includes the JavaScript.
Listing A
<html>
<head>
<title>JavaScript Date Object</title>
</head><body>
<script language="javascript">
var d = new Date();
varyy = d.getYear();
varyyyy = d.getFullYear();
var mm = d.getMonth() + 1;
vardd = d.getDate();
varww = d.getDay() + 1;
varudd = d.getUTCDate();
var umm = d.getUTCMonth() + 1;
varuyy = d.getUTCFullYear();
document.write("Today is: " + mm + "/" + dd + "/" + yyyy);
document.write("<br />");
document.write("It is the " + ww + " day of the week.");
document.write("<br />");
document.write("UTC Date is: " + umm + "/" + udd + "/" + uyy);
</script>
</body></html>
Listing B
<script language="javascript">
var d = new Date();
varyy = d.getYear();
varyyyy = d.getFullYear();
var mm = d.getMonth();
vardd = d.getDate();
varww = d.getDay();
varudd = d.getUTCDate();
var umm = d.getUTCMonth();
varuyy = d.getUTCFullYear();
var weekdays = new Array();
weekdays[0] = "Sunday";
weekdays[1] = "Monday";
weekdays[2] = "Tuesday";
weekdays[3] = "Wednesday";
weekdays[4] = "Thursday";
weekdays[5] = "Friday";
weekdays[6] = "Saturday";
document.write("Today is: " + weekdays[ww] + "
" + mm + "/" + dd + "/" + yyyy);
document.write("<br />");
document.write("UTC is: " + weekdays[ww] + "
" + umm + "/" + udd + "/" + uyy);
document.write("<br />");
</script>
You are not restricted to working with the current date. The Date object may be initialized with a value passed to it, like this:
var d = new Date("date value");
Using this approach, we could alter the previous example to use a specific date. Listing C presents a simple way to discover the day of the week for a given value.
Listing C
<script language="javascript">
var d = new Date("4/15/1979");
varyy = d.getYear();
varyyyy = d.getFullYear();
var mm = d.getMonth();
vardd = d.getDate();
varww = d.getDay();
varudd = d.getUTCDate();
var umm = d.getUTCMonth();
varuyy = d.getUTCFullYear();
var weekdays = new Array();
weekdays[0] = "Sunday";
weekdays[1] = "Monday";
weekdays[2] = "Tuesday";
weekdays[3] = "Wednesday";
weekdays[4] = "Thursday";
weekdays[5] = "Friday";
weekdays[6] = "Saturday";
document.write("Today is: " + weekdays[ww] + "
" + (mm + 1) + "/" + dd + "/" + yyyy);
document.write("<br />");
document.write("UTC is: " + weekdays[ww] + "
" + (umm + 1) + "/" + udd + "/" + uyy);
document.write("<br />");
</script>
The code produces the following output:Today is: Wednesday 4/15/1979 UTC is: Wednesday 4/15/1979Actually, there are four approaches to creating a Date object instance:
var d = new Date();
var d = new Date('July 4, 1976');
var d = new Date(7, 4, 1976);
var d = new Date(7, 4, 1976, 12,00,00);
We've covered the first two (notice that apostrophes or parentheses may be used). The final two use individual integer parameters using the following format (the time is optional):var d = new Date(month, day, year, hour, minutes, seconds);Another way to populate a Date object is by way of the setDate method. It provides a way to reset a Date object's value or initialize it, but it requires an actual JavaScript Date object:
var d1 = new Date();
var d2 = new Date("7/4/1976");
d1.setDate(d2.getDate());
There are more set methods for populating the various properties of the Date object, but let's cover time before discussing them.
Do you need help with Web Technologies? 



1
Sudip Dey - 05/03/08
codes are simple to understand....also give code which autorefresh the time....
I need your comments also....
» Report offensive content
2
Linda - 01/10/08
As part of my job, I add an expiration date to secured spec. The date is to be 1 week from the day someone prints or opens a specific spec. My problem is that it only works on 98 computers and not XP. I inherited the code and am not too familar with JavaScrip. Can you tell me why this does not work on XP computers?
Thanks for you time.
» Report offensive content