Date constructor

See Date object and methods in the Mozilla docs.

A JavaScript date is fundamentally specified as the number of milliseconds that have elapsed since midnight on January 1, 1970, UTC.

This date and time are not the same as the UNIX epoch (the number of seconds that have elapsed since midnight on January 1, 1970, UTC), which is the predominant base value for computer-recorded date and time values.

Get attribute from datetime

const datetime = new Date()

Just a few of the methods available:

datetime.getFullYear()
// 2021
datetime.getMonth()
// 3
datetime.getDate()
// 17

Day of week - 1 is Monday.

const datetime.getDay()
// 1

Represent datetime

const datetime = new Date()

Convert a Date instance to a string.

datetime.toISOString()
// '2001-09-09T01:46:40.000Z'
datetime.toString()
// 'Tue Mar 02 2021 13:47:43 GMT+0200 (South Africa Standard Time)'
datetime.toUTCString()
// 'Sun, 09 Sep 2001 01:46:40 GMT'
datetime.toDateString()
// 'Sun Sep 09 2001'
datetime.toTimeString()
// '03:46:40 GMT+0200 (South Africa Standard Time)'
datetime.toLocaleString()
// '09/09/2001, 03:46:40'

Unix timestamp from datetime:

const datetime = new Date()

// Milliseconds:
const ts = datetime.getTime()
// 1648715729097

// Convert to seconds:
parseInt(ts/1000)
// 1648715729

Get current date and time

Using Date

Create an Date object with type `object. If you don’t pass a value to the constructor, you get the current date and time.

const datetime = new Date()

Then use methods on it.

datetime.toISOString()
// 2021-03-02T11:47:43.759Z

If you omit new keyword, you get a string. This is not so useful to work with.

Date()
// "Mon Apr 19 2021 10:43:22 GMT+0200 (South Africa Standard Time)"

It is a shortcut for calling new Date().toString().

Using Date.now

Create unix timestamp for current time in milliseconds. See earlier section for converting to seconds.

Date.now()
// 1618822230513

That is the same as using Date constructor but shorter and cleaner.

Date.now() === (new Date()).getTime()
// true

UTC constructor

From Mozilla docs

The Date.UTC() method accepts parameters similar to the Date constructor, but treats them as UTC. It returns the number of milliseconds since January 1, 1970, 00:00:00 UTC.

Syntax:

Date.UTC(year[, month[, day[, hour[, minute[, second[, millisecond]]]]]])

Example:

Date.UTC(2020, 1, 2, 3, 4, 5)
// 1580612645000

Example use with Date constructor:

new Date(Date.UTC(2020, 1, 2, 3, 4, 5))
// Date Sun Feb 02 2020 05:04:05 GMT+0200 (South Africa Standard Time)

Timestamp

Convert timestamp to datetime.

Timestamp is in milliseconds, otherewise it is like unix timestamp (which is in seconds).

const datetime = new Date(1000000000000)
// Sun Sep 09 2001 03:46:40 GMT+0200 (South Africa Standard Time)

Create unix timestamp from string.

Date.parse('1970-01-02')
// 86400000

Date.parse('1970-01-01 02:00') // Currently at GMT+2
// 0

Date.parse('1970-01-01 03:00')
// 3600000

Date.parse('2021-02-03 04:05:01')
// 1612317901000

Calculate time relative to now

NB. Use of 1000 because JS uses milliseconds not seconds.

const fiveYears = 1000 * 60 * 60 * 24 * 365 * 5
fiveYears
// 157680000000

const expiry = Date.now() + fiveYears;
expiry
// 1776618462014

new Date(expiry)
// Sun Apr 19 2026 19:07:42 GMT+0200 (South Africa Standard Time)

Remember the new keyword or you’ll just get the current time even with an argument passes.

Here using .setYear to set an expiry date in 5 years time.

const expiry = new Date() // Now
expiry
// Date Tue Apr 20 2021 19:28:52 GMT+0200 (South Africa Standard Time)

const currYear = expiry.getFullYear()
currYear
// 2021

expiry.setYear(currYear + 5);

expiry
// Date Mon Apr 20 2026 19:28:52 GMT+0200 (South Africa Standard Time)

expiry.toGMTString() // now 2026!
// "Mon, 20 Apr 2026 17:28:52 GMT"

Here setting an expiry date in seconds time. Note that setting a seconds value of 60 or higher makes minutes increase.

const a = new Date()

a
// Date Mon Sep 13 2021 12:09:40 GMT+0200 (South Africa Standard Time)

const currSeconds = a.getSeconds()
// 40

a.setSeconds(currSeconds + 60)

a
// Date Mon Sep 13 2021 12:10:40 GMT+0200 (South Africa Standard Time)