Skip to content

Instantly share code, notes, and snippets.

@danalloway
Last active March 1, 2020 17:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danalloway/17b48fddab9028432c68 to your computer and use it in GitHub Desktop.
Save danalloway/17b48fddab9028432c68 to your computer and use it in GitHub Desktop.
Calculate DST in Javascript
/**
* Daylight Savings Time
*
* Since 2007 DST begins on the second sunday of March,
* and ends on the first sunday of November.
*
* @see http://www.nist.gov/pml/div688/dst.cfm
* @author Dan Alloway <dan@micahsix.com>
*/
var currentDate = new Date();
var currentYear = currentDate.getFullYear();
// DST Start
var firstOfMarch = new Date(currentYear, 2, 1);
var daysUntilFirstSundayInMarch = (7 - firstOfMarch.getDay()) % 7;
var secondSundayInMarch = firstOfMarch.getDate() + daysUntilFirstSundayInMarch + 7;
var dstStartDate = new Date(currentYear, 2, secondSundayInMarch);
// DST End
var firstOfNovember = new Date(currentYear, 10, 1);
var daysUntilFirstSundayInNov = (7 - firstOfNovember.getDay()) % 7;
var firstSundayInNovember = firstOfNovember.getDate() + daysUntilFirstSundayInNov;
var dstEndDate = new Date(currentYear, 10, firstSundayInNovember);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment