Skip to content

Instantly share code, notes, and snippets.

@atomless
Created May 30, 2014 18:47
Show Gist options
  • Save atomless/c7b52542f5f0c0dfd965 to your computer and use it in GitHub Desktop.
Save atomless/c7b52542f5f0c0dfd965 to your computer and use it in GitHub Desktop.
setDate vs setUTCDate
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)
@atomless
Copy link
Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment