Skip to content

Instantly share code, notes, and snippets.

@danalloway danalloway/dst.js
Last active May 15, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.