The Date object in JavaScript, accessing month, day ...

A date is an object whose base is a number of milliseconds and that is displayed in a variety of formats by converting milliseconds in year, month, day ...

The JavaScript version 1.3 standard extends the possibilities of the object. It is compatible with any browsers, even old ones.

An object Date is created by a call to a constructor whose format is as follows:

new Date()
new Date(millisecondes)

new Date(year, month, day [, hour, minutes, seconds, milliseconds])  // arguments are integers
new Date(text)  // a string containing a date

If there is no argument, the default is the current day and time, when the constructor is called.

In case the argument is a date in the textual form, its format must be recognised by the Date.parse method.
For example:

Apr 16, 2008     

The parse method also recognizes the IETF format, as:

Mon, 16 Apr 2008 12:20:05 GMT

The number of milliseconds is since January 1, 1970. Milliseconds are useful for durations, otherwise using dates is preferable.

UTC and GMT formats are supported

GMT means Greenwich Mean Time. It's a solar time measured Observatory at Greenwich, England.

UTC means Coordinated Universal Time. Unit is an international standard which replaces GMT, which is based on atomic time rather than on the Earth's rotation. It is defined by ISO 8601.

Calculating the difference between two dates

The Date object supports arithmetic operations. In practice, we use mostly subtraction for the time elapsed between two dates.

<script>
var start = new Date(1000, 01, 01);
var finish = new Date(2011, 10, 15);
var diff = new Number((finish.getTime() - start.getTime()) / 31536000000).toFixed(0);
document.write(diff);
</script>

Simple way to know the date of the day

It is obtained by creating a Date object without argument. For instance with the following script:

<script type="text/javascript">
	document.write(new Date());
</script>

That shows:

And the day of the week

The day of the week is displayed with an array of names, and the getDay() method which returns the number starting from 1.

<script type="text/javascript">
  var j = new Array( "Monday", "Tuesday, "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" );
  var d = new Date();
  document.write(j[d.getDay() - 1]);
</script>

Methods of date in JavaScript

Online demo of using the Date object in JavaScript, with a date as a number in milliseconds, or as a string in a format recognized by the parse method.

Enter a date


Descriptions of methods

Getting data

getDay: Day of the week (0-6) starting in sunday.

getFullYear: Year in 4 digits.

getMonth: Month in digits, from 0 to 11, zero for January.

getDate: Day of the month (1-31).

getHour: Hour.

getMinutes: Minutes.

getSeconds: Seconds in the minute for the given date.

getMilliSeconds: Number of milliseconds, from 0 to 999 in the second for the given date.

getTime: Number of milliseconds since January 1, 1970.

toString: The date as a string.

toDateString: Return the date part with the day of the week (English).

toLocaleDateString: Date part localized.

Assignment

setFullYear, setMounth, setDate, setHours, setMinutes, setSeconds, setMilliseconds.

Change data to a Date object.

setTime: Assign a number of milliseconds since January 1, 1970.

© 2008-2012 Xul.fr