050
JavaScript Date Methods Cheatsheet
Get Methods
Method | Return |
---|---|
getDate | 2 |
getDay | 6 |
getFullYear | 2023 |
getHours | 10 |
getMilliseconds | 197 |
getMinutes | 18 |
getMonth | 11 |
getSeconds | 25 |
getTime | 1701479905197 |
getTimezoneOffset | -540 |
getUTCDate | 2 |
getUTCDay | 6 |
getUTCFullYear | 2023 |
getUTCHours | 1 |
getUTCMilliseconds | 197 |
getUTCMinutes | 18 |
getUTCMonth | 11 |
getUTCSeconds | 25 |
To Methods
Method | Return |
---|---|
toDateString | Sat Dec 02 2023 |
toISOString | 2023-12-02T01:18:25.197Z |
toJSON | 2023-12-02T01:18:25.197Z |
toLocaleDateString | 12/2/2023 |
toLocaleString | 12/2/2023, 10:18:25 AM |
toLocaleTimeString | 10:18:25 AM |
toString | Sat Dec 02 2023 10:18:25 GMT+0900 (Japan Standard Time) |
toTimeString | 10:18:25 GMT+0900 (Japan Standard Time) |
toUTCString | Sat, 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)