Last active
April 15, 2024 09:25
-
-
Save alirussell/c52f58103db52f00fc70 to your computer and use it in GitHub Desktop.
Check for british summer time (BST) in JavaScript
This file contains hidden or 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
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(); | |
} |
Thanks this was a big help with storing dates in MongoDB, because you have to adjust in relation to UTC.
Hey @alirussell , great stuff!
But, while I'm trying it out... shouldn't line 13 be ends.setHours(1)
?
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
Thank you for this! It was really helpful in my pieced-together sunrise/sunset calculation.