Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

JS Dates

JavaScript Date Objects enable manipulation and representation of dates.

April 27, 2024, 6:11:25 PM (GMT+10:00 Australian Eastern Standard Time)

Example

const d = new Date();
const d = new Date(“2022-03-25”);

Note:

Date objects are static; they do not update in real-time like a clock. 

While the computer’s clock is continuously ticking, date objects remain fixed unless explicitly changed.

JavaScript Date Output

JavaScript typically utilizes the browser’s time zone settings and presents dates as comprehensive text strings, such as:

Saturday, April 27, 2024, 6:11:25 PM GMT+10:00 (Australian Eastern Standard Time)

Creating Date Objects

Date objects are generated using the new Date() constructor, offering 9 different methods for creating a new date object

new Date()
new Date(date string)

new Date(year,month)
new Date(year,month,day)
new Date(year,month,day,hours)
new Date(year,month,day,hours,minutes)
new Date(year,month,day,hours,minutes,seconds)
new Date(year,month,day,hours,minutes,seconds,ms)

new Date(milliseconds)

JavaScript new Date()

Using new Date() generates a date object representing the current date and time.

Example

const d = new Date();

new Date(date string)

The new Date(date string) constructor creates a date object based on a provided date string.

Examples

const d = new Date(“October 13, 2014 11:13:00”);
const d = new Date(“2022-03-25”);

new Date(year, month, …)

The new Date(year, month, …) constructor generates a date object with a defined date and time.

Seven numerical parameters represent the year, month, day, hour, minute, second, and millisecond (in that order).

Example

const d = new Date(201811241033300);

Note: That in JavaScript, months are counted from 0 to 11, where January is represented as 0 and December as 11.

If you specify a month higher than 11, JavaScript will not throw an error; instead, it will add the overflow to the next year.

Indicating:

const d = new Date(20181524103330);

Has the same effect as:

const d = new Date(2019324103330);

If you specify a day higher than the maximum for that month, it will overflow into the next month without causing an error.

Defining:

const d = new Date(2018535103330);

Has the same effect as:

const d = new Date(201865103330);

Using 6, 4, 3, or 2 Numbers

Six digits indicate the year, month, day, hour, minute, and second.

Example

const d = new Date(20181124103330);

Five digits denote the year, month, day, hour, and minute.

Example

const d = new Date(201811241033);

Four digits represent the year, month, day, and hour.

Example

const d = new Date(2018112410);

Three digits represent the year, month, and day.

Example

const d = new Date(20181124);

Two digits represent the year and month.

Example

const d = new Date(201811);

If you provide only one parameter, it will be interpreted as milliseconds, so you can’t omit the month.

Example

const d = new Date(2018);

Previous Century

Years specified with one or two digits will be interpreted as the 20th century (19xx).

Example

const d = new Date(991124);

Example

const d = new Date(91124);

JavaScript Stores Dates as Milliseconds 

JavaScript stores dates as the number of milliseconds elapsed since January 1, 1970.

The zero time reference is January 1, 1970, 00:00:00 UTC.

A single day, equivalent to 24 hours, amounts to 86,400,000 milliseconds.

As of now, the time is represented by 1719031847807 milliseconds elapsed since January 1, 1970.

new Date(milliseconds)

Using new Date(milliseconds) creates a new date object with the specified number of milliseconds added to the zero time reference.

Example

Adding 100,000,000,000 milliseconds to January 1, 1970, results in:

const d = new Date(100000000000);

Subtracting 100,000,000,000 milliseconds from January 1, 1970, results in:

const d = new Date(-100000000000);

Adding 24 hours to January 1, 1970, results in:

const d = new Date(24 * 60 * 60 * 1000);
// or
const d = new Date(86400000);

January 1, 1970 plus 0 milliseconds is:

const d = new Date(0);

Date Methods

Date methods facilitate operations on date objects, enabling you to retrieve and modify various components such as the year, month, day, hour, minute, second, and millisecond. These operations can be performed in either local time or Coordinated Universal Time (UTC), also known as Greenwich Mean Time (GMT).

The upcoming chapters will delve into Date methods and their interaction with time zones.

Displaying Dates

By default, JavaScript will display dates using the toString() method, providing a string representation that includes the time zone. The format adheres to the specifications outlined in the ECMAScript standard.

Example

Sat Apr 27 2024 18:11:25 GMT+1000 (Australian Eastern Standard Time)

When a date object is displayed in HTML, it is automatically converted to a string using the toString() method.

Example

const d = new Date();
d.toString();

The toDateString() method converts a date into a more human-readable format.

Example

const d = new Date();
d.toDateString();

The toUTCString() method converts a date into a string representation using the UTC standard.

Example

const d = new Date();
d.toUTCString();

The toISOString() method converts a date into a string representation using the ISO standard.

Example

const d = new Date();
d.toISOString();