Contact Us or call 1-877-932-8228


The Date object has methods for manipulating dates and times. JavaScript stores dates as the number of milliseconds since January 1, 1970. The sample below shows the different methods of creating date objects, all of which involve passing arguments to the Date() constructor.

Code Sample:

---- C O D E   O M I T T E D ----

<h1>Date Object</h1>
<h2>New Date object with current date and time</h2>
//Syntax: new Date();
var now = new Date();
<script type="text/javascript">
	var now = new Date();

<h2>New Date object with specific date and time</h2>
//Syntax: new Date("month dd, yyyy hh:mm:ss);
var redSoxWin = new Date("October 21, 2004 12:01:00");
<script type="text/javascript">
	var redSoxWin = new Date("October 21, 2004 12:01:00");

//Syntax: new Date(yyyy, mm, dd, hh, mm, ss, ms);
redSoxWin = new Date(2004, 9, 21, 12, 01, 00, 00);
<script type="text/javascript">
	redSoxWin = new Date(2004, 9, 21, 12, 01, 00, 00);

This page is shown in a browser below.

A few things to note:

  1. To create a Date object containing the current date and time, the Date() constructor takes no arguments.
  2. When passing the date as a string to the Date() constructor, the time portion is optional. If it is not included, it defaults to 00:00:00. Also, other date formats are acceptable (e.g, "4/14/2011" and "14-04-2011").
  3. When passing date parts to the Date() constructor, dd, hh, mm, ss, and ms are all optional. The default of each is 0.
  4. Months are numbered from 0 (January) to 11 (December). In the example above, 9 represents October.

Some common date methods are shown below. In all the examples, the variable rightNow contains "Thu Apr 14 00:23:54:650 EDT 2011".

Common Date Methods
Method Description
getDate() Returns the day of the month (1-31).
//Returns 14
getDay() Returns the day of the week as a number (0-6, 0=Sunday, 6=Saturday).
//Returns 4
getMonth() Returns the month as a number (0-11, 0=January, 11=December).
//Returns 3
getFullYear() Returns the four-digit year.
//Returns 2011
getHours() Returns the hour (0-23).
//Returns 0
getMinutes() Returns the minute (0-59).
//Returns 23
getSeconds() Returns the second (0-59).
//Returns 54
getMilliseconds() Returns the millisecond (0-999).
//Returns 650
getTime() Returns the number of milliseconds since midnight January 1, 1970.
//Returns 1113452634650
getTimezoneOffset() Returns the time difference in minutes between the user's computer and GMT.
//Returns 240
toLocaleString() Returns the Date object as a string.
//Returns Thursday, April 14,
//2011 12:23:54 AM
toGMTString() Returns the Date object as a string in GMT timezone.
//Returns Thu, 14 Apr 2011
//04:23:54 UTC 

You can see these examples in a browser by opening BuiltInObjects/Demos/DateMethods.html.