Skip to content

Instantly share code, notes, and snippets.

@Mellen
Last active September 7, 2021 08:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mellen/61bada8b3e6d0763c9f95da55d5f0b4b to your computer and use it in GitHub Desktop.
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
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;
}
@Mellen
Copy link
Author

Mellen commented Sep 6, 2021

boundaryDay allows you to set which day is the start of the week. It defaults to 0 (Sunday).
If date1 is later than date2 the dates get swapped around.

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