Web/JavaScript

[JavaScript] Date Object

llHoYall 2022. 7. 9. 15:31

The Date object represents a single moment in time in a platform-independent format.

It contains a Number that represents milliseconds since 1970/01/01 00:00:00 UTC.

There is a difference in the result representation depending on the browser environment and node.js environment.

Date Constructor

There are 4 ways to create a Date instance.

new Date()

It returns a date instance with the current date and time.

const date = new Date();
// Fri Jul 08 2022 22:16:39 GMT+0900 (Korean Standard Time)

If we call as a function, it returns the current date and time as a string.

Date();
// 'Fri Jul 08 2022 22:17:53 GMT+0900 (Korean Standard Time)'

new Date(millisecond)

It returns a date instance for the elapsed time as much as the input milliseconds from the base time.

const date = new Date(86400000); // 86400000 is one day.
// Fri Jan 02 1970 09:00:00 GMT+0900 (Korean Standard Time)

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

It returns a date instance with the input date and time.

const date = new Date(2022, 7);
// Mon Aug 01 2022 00:00:00 GMT+0900 (Korean Standard Time)
const date = new Date(2022, 7, 8, 12, 30, 45, 789);
// Mon Aug 08 2022 12:30:45 GMT+0900 (Korean Standard Time)

new Date(dateString)

It returns a date instance with the data and time entered with a string.

It is the best way from a readability perspective.

const date = new Date('July 8 2022 12:30:45');
// Fri Jul 08 2022 12:30:45 GMT+0900 (Korean Standard Time)
const date = new Date('2022/07/08/12:30:45');
// Fri Jul 08 2022 12:30:45 GMT+0900 (Korean Standard Time)

Date Static Method

now()

This method returns the numeric value corresponding to the current time elapsed since the base time, with leap seconds ignored.

Date.now(); // 1657286966353

parse()

This method parses a string representation of a date and returns the number of milliseconds since the base time, with leap seconds ignored.

Date.parse('2022/07/08/12:30:45'); // 1657251045000
Date.parse('Jul 8, 2022 12:30:45 UTC'); // 1657283445000

UTC()

This method accepts the same parameters as the longest form of the constructor and returns the number of milliseconds since the base time, with leap seconds ignored.

Date.UTC(2022, 7, 8); // 1659916800000
Date.UTC('2022/07/08'); // NaN

Date Instance Method

Date related Methods

We can easily set and get a year, month, date, and day.

0 on the month means January.

0 on the day of the week means Sunday.

let date = new Date();

date.setFullYear(2022);
date.setMonth(6);
date.setDate(8);

console.log(date.getFullYear()); // 2022
console.log(date.getMonth()); // 6 (July)
console.log(date.getDate()); // 8
console.log(date.getDay()); // 5 (Friday)

Subunits can also be set.

date.setFullYear(2022, 6, 8);
date.setMonth(6, 8);

Time related Methods

We can also set and get an hour, minute, second, and millisecond.

let date = new Date();

date.setHours(12);
date.setMinutes(30);
date.setSeconds(45);
date.setMilliseconds(789);

console.log(date.getHours()); // 12
console.log(date.getMinutes()); // 30
console.log(date.getSeconds()); // 45
console.log(date.getMilliseconds()); // 789

Subunits can also be set.

date.setHours(12, 30, 45, 789);
date.setMinutes(30, 45, 789);
date.setSeconds(45, 789);
date.setMilliseconds(789);

for Universal Time Methods

The above methods are for the local time.

If we input UTC in the name of the methods, it can be used for universal time.

date.setUTCFullYear(2022);
date.setUTCMonth(6);
date.setUTCDate(8);

date.getUTCFullYear();
date.getUTCMonth();
date.getUTCDate();
date.getUTCDay();

date.setUTCHours(12);
date.setUTCMinutes(30);
date.setUTCSeconds(45);
date.setUTCMilliseconds(789);

date.getUTCHours();
date.getUTCMinutes();
date.getUTCSeconds();
date.getUTCMilliseconds();

Date and Time related Methods

There is also a way to set and get a date and time together.

It sets and gets to the time represented by a number of milliseconds since the base time.

let date = new Date();

date.setTime(1657251045000);

console.log(date.getTime()); // 1657251045000

The getTimezoneOffset() method returns the difference, in minutes, between a date as evaluated in the UTC time zone, and the same date as evaluated in the local time zone.

new Date().getTimezoneOffset() / 60; // -9

UTC is 9 hours behind KST.

toString Methods

These methods return the date to the specified string.

const date = new Date('2022/07/08 12:30:45');

console.log(date.toString()); // Fri Jul 08 2022 12:30:45 GMT+0900 (Korean Standard Time)
console.log(date.toDateString()); // Fri Jul 08 2022
console.log(date.toTimeString()); // 12:30:45 GMT+0900 (Korean Standard Time)
console.log(date.toISOString()); // 2022-07-08T03:30:45.000Z

These methods return a string with a language-sensitive representation of the date.

const date = new Date('2022/07/08 12:30:45');

console.log(date.toLocaleString()); // 7/8/2022, 12:30:45 PM
console.log(date.toLocaleString('ko-kr')); // 2022. 7. 8. 오후 12:30:45
console.log(date.toLocaleString('ja-jp')); // 2022/7/8 12:30:45

console.log(date.toLocaleTimeString()); // 12:30:45 PM
console.log(date.toLocaleTimeString('ko-kr')); // 오후 12:30:45
console.log(date.toLocaleTimeString('ja-jp')); // 12:30:45