JavaScript: Date Tutorial

By Xah Lee. Date: . Last updated: .

First, here's a quick usage example:

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

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

console.log( hh + ":" + mm + ":" + ss ); // 8:15:51
console.log( dd.getFullYear() ); // 2020

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

console.log( dd.getMonth() ); // 8
// warning: Jan is 0, Feb is 1, etc

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

Creating Date Object

Date Constructor

Static Methods

Date()
Return a string that represents the datetime this code is evaluated. Any arguments are ignored. Sample return value: Sun Sep 06 2020 08:19:41 GMT-0700 (Pacific Daylight Time)
Date.now()
Return the millisecond representation of date when this code is called. Same as (new Date()).getTime(). Sample return value: 1428601360919.
Date.parse(date_time_string)
Parse a date time string, return the millisecond representation of that date. [see Date.parse]
Date.UTC(…)
Return a date object. The arguments are the same as new Date(), but interprets them as UTC.

For full detail, see: 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
const 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.

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

Use toString() to get a string representation.

const 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 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.

const 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 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. )

JavaScript Date

BUY
Ξ£JS
JavaScript in Depth

JavaScript in Depth

Basic Syntax

Value Types

Variable

String

Function

Property

Object and Inheritance

Array

Constructor/Class

Iterable 🌟

Misc