Skip to content

Instantly share code, notes, and snippets.

@alirussell
Last active April 15, 2024 09:25
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alirussell/c52f58103db52f00fc70 to your computer and use it in GitHub Desktop.
Save alirussell/c52f58103db52f00fc70 to your computer and use it in GitHub Desktop.
Check for british summer time (BST) in JavaScript
function lastSunday(month, year) {
var d = new Date();
var lastDayOfMonth = new Date(Date.UTC(year || d.getFullYear(), month+1, 0));
var day = lastDayOfMonth.getDay();
return new Date(Date.UTC(lastDayOfMonth.getFullYear(), lastDayOfMonth.getMonth(), lastDayOfMonth.getDate() - day));
}
function isBST(date) {
var d = date || new Date();
var starts = lastSunday(2, d.getFullYear());
starts.setHours(1);
var ends = lastSunday(9, d.getFullYear());
ends.setHours(1);
return d.getTime() >= starts.getTime() && d.getTime() < ends.getTime();
}
@Polyducks
Copy link

Thank you for this! It was really helpful in my pieced-together sunrise/sunset calculation.

@justcoder1
Copy link

Thanks this was a big help with storing dates in MongoDB, because you have to adjust in relation to UTC.

@Mefioo9
Copy link

Mefioo9 commented Nov 30, 2021

Hey @alirussell , great stuff!
But, while I'm trying it out... shouldn't line 13 be ends.setHours(1)?

@liam-jones-lucout
Copy link

Made a new version that seems to work and is a bit simpler: https://gist.github.com/liam-jones-lucout/1c8b19ad6fcaf4103bb4b6af97877cd7

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