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/1979
Actually, 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? Gain advice from Builder AU forums

Related links

Comments

1

Sudip Dey - 05/03/08

codes are simple to understand....also give code which autorefresh the time....
I need your comments also....

<html>
<head>
<title>Digital Clock</title>
<style>
<!--
.styling{
background-color:black;
color:lime;
font: bold 15px MS Sans Serif;
padding: 3px;
}
-->
</style>
</head>

<body>
<span id="digitalclock" class="styling"></span>

<script>
<!--

/*****************************************
* LCD Clock script- by Javascriptkit.com
* Featured on/available at http://www.dynamicdrive.com/
* This notice must stay intact for use
*****************************************/

var alternate=0
var standardbrowser=!document.all&&!document.getElementById

if (standardbrowser)
document.write('<form name="tick"><input type="text" name="tock" size="6"></form>')

function show(){
if (!standardbrowser)
var clockobj=document.getElementById? document.getElementById("digitalclock") : document.all.digitalclock
var Digital=new Date()
var hours=Digital.getHours()
var minutes=Digital.getMinutes()
var dn="AM"

if (hours==12) dn="PM"
if (hours>12){
dn="PM"
hours=hours-12
}
if (hours==0) hours=12
if (hours.toString().length==1)
hours="0"+hours
if (minutes<=9)
minutes="0"+minutes

if (standardbrowser){
if (alternate==0)
document.tick.tock.value=hours+" : "+minutes+" "+dn
else
document.tick.tock.value=hours+" "+minutes+" "+dn
}
else{
if (alternate==0)
clockobj.innerHTML=hours+"<font color='lime'>&nbsp;:&nbsp;</font>"+minutes+" "+"<sup style='font-size:1px'>"+dn+"</sup>"
else
clockobj.innerHTML=hours+"<font color='black'>&nbsp;:&nbsp;</font>"+minutes+" "+"<sup style='font-size:1px'>"+dn+"</sup>"
}
alternate=(alternate==0)? 1 : 0
setTimeout("show()",1000)
}
window.onload=show

//-->
</script>
</body>
</html>

» Report offensive content

Leave a comment

You must read and type the 6 chars within 0..9 and A..F

* indicates mandatory fields.

1

Sudip Dey - 03/05/08

codes are simple to understand....also give code which autorefresh the time.... I need your comments also.... <html> <head> <title>Digital Clock</title> <style> <!-- .styling{ background-color:black; color:lime; font: bold 15px MS Sans ... more

Log in


Sign up | Forgot your password?

  • Chris Duckett Safari gets Gears

    Since its release in May last year, Gears has supported only Internet Explorer and Firefox browsers. With the addition of Safari into the Gears fold, it closes the loop of major browsers to support Gears Read more »

    -- posted by Chris Duckett

  • Renai LeMay MyPerfect.com.au has potential

    Victorian Web start-up My Perfect has a strong story and rationale for why it will succeed. But it has to overcome some challenges and design flaws first. Read more »

    -- posted by Renai LeMay

  • Brendon Chase Blog against poverty

    Worldwide Blog Action Day is 15 October, in 2008 the goal is to raise awareness and conversation around the worldwide topic of poverty and in the process raise money for the cause. Who's in? Read more »

    -- posted by Brendon Chase

What's on?