Zac Fukuda
050

JavaScript Date Methods Cheatsheet

Date - JavaScript | MDN

Get Methods

MethodReturn
getDate2
getDay6
getFullYear2023
getHours10
getMilliseconds197
getMinutes18
getMonth11
getSeconds25
getTime1701479905197
getTimezoneOffset-540
getUTCDate2
getUTCDay6
getUTCFullYear2023
getUTCHours1
getUTCMilliseconds197
getUTCMinutes18
getUTCMonth11
getUTCSeconds25

To Methods

MethodReturn
toDateStringSat Dec 02 2023
toISOString2023-12-02T01:18:25.197Z
toJSON2023-12-02T01:18:25.197Z
toLocaleDateString12/2/2023
toLocaleString12/2/2023, 10:18:25 AM
toLocaleTimeString10:18:25 AM
toStringSat Dec 02 2023 10:18:25 GMT+0900 (Japan Standard Time)
toTimeString10:18:25 GMT+0900 (Japan Standard Time)
toUTCStringSat, 02 Dec 2023 01:18:25 GMT

Constructor

Be careful when you construct a new Date object:

new Date("2023-12-02").toString()
// Sat Dec 02 2023 09:00:00 GMT+0900 (Japan Standard Time)

new Date(2023, 11, 2).toString()
// Sat Dec 02 2023 00:00:00 GMT+0900 (Japan Standard Time)

new Date(1701475200000).toString()
// Sat Dec 02 2023 09:00:00 GMT+0900 (Japan Standard Time)

As you can see, when the string in ISO format is given, the constructor treats the parameter as an UTC. When the year, monthindex, and day—so on—are given, it treats the parameters as a local time.

To get a local time Date object in ISO string, you can add a timezone offset:

new Date("2023-12-02T00:00:00.000+09:00").toString()
// Sat Dec 02 2023 00:00:00 GMT+0900 (Japan Standard Time)