One of the trickier aspects of development is working with time and date values; it seems like each language or platform has its own format. With Web development, you can utilise server-side code to manipulate dates, or you can utilise JavaScript's Date object. In today's column, we take a closer look at this object.

Working with dates
It is simple to get going with time and date values in JavaScript. It begins with creating an instance of the Date object:

var d = new Date();
This returns an instance of the Date object in the variable d using the current date and time. The Date object includes numerous methods and properties for accessing and manipulating its value. The following list includes methods for accessing date values:
  • getDate(): Returns the day of the month.
  • getDay(): Returns the day of the week. The week begins with Sunday (0 - 6).
  • getFullYear(): Returns the four-digit year.
  • getMonth(): Returns the month.
  • getYear(): Returns the two-digit year.
  • getUTCDate(): Returns the day of the month according to the Coordinated Universal Time (UTC).
  • getUTCMonth(): Returns the month according to the UTC (0 - 11).
  • getUTCFullYear(): Returns the four-digit year according to the UTC.
Note: Wikipedia defines UTC as a high-precision atomic time standard which approximately tracks Universal Time (UT).

An important note regarding JavaScript and dates is it uses the number of milliseconds from midnight January 1, 1970 to store dates. This is called the epoch, and any dates and times before this date are not allowed.

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

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.

/* Example of date arithmetic. */
/* Create a date object containing the current date. */
var d1 = new Date();
/* num contains the numeric representation of the current date. */
var num = d1.valueOf();
/* Add seven days to today’s date. */
/* 1000 ms / sec; 60 sec / min; 60 min / hour; 24 hours / day; 7 days */
num += 1000 * 60 * 60 * 24 * 7;
/* Create our new date that is 7 days ahead of the current date. */
var d2 = (new Date(num));
/* Add text. */
var d3 = "Document Expires:";
/* Print out the current date and our new date using util.printd */
var f = this.getField("Today1");
f.value = d3 +(util.printd("mm/dd/yyyy", d2));

» Report offensive content

Leave a comment

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

* indicates mandatory fields.

2

Linda - 10/01/08

As part of my job, I add an expiration date to secured spec. The date is to be 1 week ... more

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?

  • Staff Microsoft shows off IE9 preview

    This week, highlights from Microsoft's MIX10 conference and more in the Roundup. Read more »

    -- posted by Staff

  • Chris Duckett IE9's H.264 vote killed Ogg

    In a split decision by the judges, the winner of the W3C/WHATWG video codec consensus is H.264, taking home the future of video playback on the internet while loser Ogg goes home with nothing but thoughts of what might have been. Read more »

    -- posted by Chris Duckett

  • Staff Google launches Apps Marketplace

    Google launches and app store, while Mozilla plans to re-write its open-source license. More of this week's news in the Roundup. Read more »

    -- posted by Staff

What's on?

  • Optus Deal

    Broadband + home phone + PlayStation®3 in a single package price!