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
boundaryDayallows you to set which day is the start of the week. It defaults to 0 (Sunday).If
date1is later thandate2the dates get swapped around.