JS: Date Tutorial

By Xah Lee. Date: . Last updated: .

First, here's a quick usage example:

// create a date instance of current date time
var dd = new Date();

var hh = dd.getHours();
var mm = dd.getMinutes();
var ss = dd.getSeconds();

console.log(
    hh + ":" + mm + ":" + ss
); // sample output: 16:49:44

console.log ( dd.getFullYear() ); // 2016

console.log ( dd.getDate() ); // 23
// first day of month is 1.

console.log ( dd.getMonth() ); // 11
// warning: January is 0, December is 12

console.log ( dd.getDay() ); // 5
// Sunday is 0. Saturday is 6.

Creating Date Object

JS: Date Constructor

Static Methods

Date() → Returns a string that represents the datetime this code is evaluated. Any arguments are ignored. This is equivalent to (new Date()).toString(). Sample return value: "Wed Apr 08 2015 08:36:13 GMT-0700 (PDT)".

console.log(
    Date()
);
// Tue Apr 26 2016 16:53:05 GMT-0700 (PDT)

Date.now() → returns the millisecond representation of date when this code is called. Same as (new Date()).getTime(). Sample return value: 1428601360919.

Date.parse(date_time_string) → Parses date_time_string, returns the millisecond representation of that date.

[see JavaScript Date.parse]

Date.UTC(…) → Returns a date object. The arguments are the same as new Date(), but interprets them as UTC.

For full detail, see:

JS: Date Object

Date Object Get Methods

Most date get methods have 2 versions. One returns local time info, one returns UTC time info. For example, getHours() vs getUTCHours().

// example of using Date get methods
var dt = new Date("2017-06-13T21:00:01.000Z");

console.log( dt.getFullYear() ); // 2017
console.log( dt.getUTCMonth() ); // 5
console.log( dt.getUTCDate() ); // 13
console.log( dt.getUTCDay() ); // 2 (Tuesday)
console.log( dt.getUTCHours() ); // 21
console.log( dt.getUTCMinutes() ); // 0
console.log( dt.getUTCSeconds() ); // 1

console.log( dt.getFullYear() ); // 2017
console.log( dt.getMonth() ); // 5
console.log( dt.getDate() ); // 13
console.log( dt.getDay() ); // 2 (Tuesday)
console.log( dt.getHours() ); // 14
console.log( dt.getMinutes() ); // 0
console.log( dt.getSeconds() ); // 1

Use date.getTime() to get milliseconds.

let d = new Date();
console.log ( d. getTime ( ) ); // 1485919031443

Use toString() to get a string representation.

let d = new Date();

// local
console.log ( d.toString ( ) ); // Tue Jan 31 2017 19:19:22 GMT-0800 (PST)

// GMT
console.log ( d.toUTCString ( ) ); // Wed, 01 Feb 2017 03:19:47 GMT

[see JS: Date.prototype]

Date Object Set Methods

You can also set a date object's year, month, or any of the parts. Here's a example of setting the minutes part.

var d = new Date("2017-04-08T06:00:10.000Z");
console.log ( d ); // 2017-04-08T06:00:10.000Z
d.setMinutes(30);
console.log ( d ); // 2017-04-08T06:30:10.000Z

For full list of set methods, see JS: Date.prototype

What is the difference between UTC and GMT?

For practical purposes, they are the same.

UTC (Coordinated Universal Time) is a time standard based on atomic clock.

GMT (Greenwich Mean Time) is a older time standard.

“Zulu time” is a term used in military and is the same as GMT. (GMT and UTC has a abbrev of Z, because Z is like zero. Z in NATO phonetic alphabet is read as Zulu. That's why GMT/UTC is also Zulu time. )

Date Topic

  1. JS: Date Tutorial
  2. JavaScript Date.parse
  3. JS: Compute Date Range

  1. JS: Date Object
  2. JS: Date Constructor
  3. JS: Date.prototype
Liket it? Put $1 at patreon.

Or, Buy JavaScript in Depth