Created
May 30, 2014 18:47
-
-
Save atomless/c7b52542f5f0c0dfd965 to your computer and use it in GitHub Desktop.
setDate vs setUTCDate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
d = new Date('Fri May 30 2014 00:00:00 GMT+0100 (BST)'); | |
// >> Fri May 30 2014 00:00:00 GMT+0100 (BST) | |
d.setUTCDate(31); | |
// >> 1401577200000 | |
d | |
// >> Sun Jun 01 2014 00:00:00 GMT+0100 (BST) | |
d = new Date('Fri May 30 2014 00:00:00 GMT+0100 (BST)'); | |
// >> Fri May 30 2014 00:00:00 GMT+0100 (BST) | |
d.setDate(31); | |
// >> 1401490800000 | |
d | |
// >> Sat May 31 2014 00:00:00 GMT+0100 (BST) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using the setUTCDate method to set the day to the 31st only results in the date changing to the 1st of June if the GMT offset is a high enough positive value to push the clock forward past midnight into the next day. It's easy to get caught out by the nuances of setUTCDate!