DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 966,904 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
LΓ’m
LΓ’m

Posted on

Javascript Date Cheat Sheet

[Date] Setters

Method Result
.setDate (val)
.setDay (val)
.setFullYear (val)
.setMonth (val)
--- ---
.setHours (val)
.setMinutes (val)
.setSeconds (val)
.setMilliseconds (val)
--- ---
.setTime (val)
.setTimezoneOffset (val)

See the getters list.

[Date] Getters

Method Result
.getDate() 1..31
.getDay() 0..6 (sun..sat)
.getFullYear() 2014
.getMonth() 0..11
--- ---
.getHours()
.getMinutes()
.getSeconds()
.getMilliseconds()
--- ---
.getTime() ms since epoch
.getTimezoneOffset()

UTC versions are also available (eg, .getUTCDate(), .getUTCDay(), etc).

[Date] Conversion

Method Result
d.toString() "Mon Dec 29 2014 00:58:28 GMT+0800 (PHT)"
d.toTimeString() "00:58:46 GMT+0800 (PHT)"
d.toUTCString() "Sun, 28 Dec 2014 16:58:59 GMT"
--- ---
d.toDateString() "Thu Jan 10 2013"
d.toISOString() "2013-01-09T16:00:00.000Z"
d.toLocaleString() "12/29/2014, 12:57:31 AM"
d.toLocaleTimeString() "12:57:31 AM"
--- ---
d.getTime() 1419785527580

Accessing

[Date] Constructor

// Now
new Date()
Enter fullscreen mode Exit fullscreen mode
// ms since epoch
new Date(1419785527580)
Enter fullscreen mode Exit fullscreen mode
// Date format
new Date("May 17, 1995 03:24:00")
Enter fullscreen mode Exit fullscreen mode
// ISO date format
new Date("2013-03-01T01:10:00")
Enter fullscreen mode Exit fullscreen mode
new Date(2014, 2, 1, 13, 0, 59, 0)
Enter fullscreen mode Exit fullscreen mode

Date

Reference

Top comments (0)

🀯

"I made 10x faster JSON.stringify() functions, even type safe"

☝️ Must read for JS devs