The Date() object

There are four ways of instantiating a date object:

new Date();    // Today's date, now.
new Date(milliseconds);
new Date(dateString);
new Date(year, month, day, hours, minutes, seconds, milliseconds);
    //most parameters here are optional. Not specifying causes 0 to be passed in.

Here are a few examples of instantiating a date:

var today = new Date();
var birthday = new Date("March 22, 1976 09:25:00");
var birthday = new Date(76,3,22);
var birthday = new Date(76,3,11,9,25,0);

Many methods exist for extracting details from the date object, for example:

  • d.getDay();
  • d.getMonth();
  • d.getYear();
  • d.getFullYear();

You can also set the properties of the object with methods. So you can create date objects for times in the future or the past. There are also methods for representing the date in different ways. For example:

var d = new Date();
document.writeln( d.toUTCString() );

The above will convert the current date into a format that is frequently used for databases and "last modified" date stamps (Universal Time code):

A similar method, toGMTString() is useful for creating future expiration times for cookies.


Reference http://www.javascriptkit.com/jsref/date.shtml