ABOUT CONTACT
Prev Page Next Page
Top

JavaScript Date Objects

➢By default, JavaScript will use the browser's time zone and display a date as a full text string.
➢There are 4 ways to create a new date object:

new Date()

➢new Date() creates a new date object with the current date and time:

EXAMPLE:

const d = new Date();

new Date(year, month, ...)

➢new Date(year, month, ...) creates a new date object with a specified date and time.
➢7 numbers specify year, month, day, hour, minute, second, and millisecond (in that order):

EXAMPLE:

const d = new Date(2022, 09, 12, 12, 29, 99, 0);

➢6 numbers specify year, month, day, hour, minute, second:

EXAMPLE:

const d = new Date(2022, 09, 12, 12, 29, 99);

➢5 numbers specify year, month, day, hour, and minute:

EXAMPLE:

const d = new Date(2022, 09, 12, 12, 29);

➢4 numbers specify year, month, day, and hour:

EXAMPLE:

const d = new Date(2022, 09, 12, 12);

➢3 numbers specify year, month, and day:

EXAMPLE:

const d = new Date(2022, 09, 12);

➢2 numbers specify year and month:

EXAMPLE:

const d = new Date(2022, 09);

➢You cannot omit month. If you supply only one parameter it will be treated as milliseconds.

EXAMPLE:

const d = new Date(2022);

Previous Century

➢One and two digit years will be interpreted as 19xx:

EXAMPLE:

const d = new Date new Date(99, 11, 24);

new Date(dateString)

➢new Date(dateString) creates a new date object from a date string:

EXAMPLE:

const d = new Date new Date("September 12, 2022 12:30:00");

new Date(milliseconds)

➢new Date(milliseconds) creates a new date object as zero time plus milliseconds:

EXAMPLE:

const d = new Date new Date(0);

Prev Page Next Page