Last active
September 7, 2021 08:10
-
-
Save Mellen/61bada8b3e6d0763c9f95da55d5f0b4b to your computer and use it in GitHub Desktop.
Detect if two dates are in the same week based on UTC time
This file contains 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 areSameWeeksUTC(date1, date2, boundaryDay=0) | |
{ | |
if(date1 > date2) | |
{ | |
let t = date1; | |
date1 = date2; | |
date2 = t; | |
} | |
if(((((date2 - date1)/1000)/3600)/24)>6) | |
{ | |
return false; | |
} | |
let day1 = date1.getUTCDay(); | |
let day2 = date2.getUTCDay(); | |
if(day1 == boundaryDay) | |
{ | |
return true; | |
} | |
if(day2 == boundaryDay) | |
{ | |
return false; | |
} | |
let d1BoundaryDist = ((day1-boundaryDay)+7)%7; | |
let d2BoundaryDist = ((day2-boundaryDay)+7)%7; | |
if(d1BoundaryDist <= d2BoundaryDist) | |
{ | |
return true; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
boundaryDay
allows you to set which day is the start of the week. It defaults to 0 (Sunday).If
date1
is later thandate2
the dates get swapped around.