-2

I want to implement a schedule that repeats every 2 weeks (Perhaps I can use Modulos for this?). For example, Monday May 14 is Day 6, whereas Monday May 21 is Day 1. Then, Monday May 28 is back to Day 6. So far, what I've come with can only check if a Day is Monday. However, that Monday could either be Day 1 or Day 6. Is there a way I can distinguish these days? I've attached a code snippet below. Thanks.

*To clarify, I am disregarding the weekends, thus, the second Monday would be Day 6, not Day 8.

var time1 = new Date(2018,4,11);
var time2 = new Date(2018,4,21);
var time3 = new Date();
var diff = time2.getDate() - time1.getDate();
var dayNum = document.getElementById("dayNumber");
var day = time2.getDay();
var isWeekend = (day == 6) || (day == 0); 

if((diff % 7 === 3) && day === 1){

 document.getElementById("dayNumber").innerHTML="Today is Day 6"; 

} 



//if day divided by 7 leaves a remainder of 3, and it's a monday, that day is either day 1 or day 6 
<p id = "dayNumber"></p>
DarkRunner
  • 449
  • 4
  • 15
  • A week has `7` days, why is the second _Monday_ numbered `6` and not `8`? – Paul S. May 12 '18 at 14:28
  • Ah, I see. The second Monday is numbered 6 because I'm disregarding weekends. I should have mentioned that in the question. Sorry for the confusion. – DarkRunner May 12 '18 at 14:30

1 Answers1

1

First, it seems like you're thinking about a pattern which repeats in 14s and trying to solve it in mod 7, which is going to be quite difficult. Instead, also work in mod 14.

Secondly, 7 days after day 1 would be day 8 (not day 6), so you need a further transform for these cases. If you don't care about Saturdays or Sundays, we could "throw them away" by mapping them all to 0. This does mean that the operation is not reversible for weekends.

Here is a function which returns

  • 0 if the input is 0 or 6 (mod 7)
  • 2 less than the input (mod 14) if the input is over 5 (mod 14)
  • the input (mod 14) if the input is 5 or less (mod 14)

So you just need to adjust for some initial offset

const day = x => {
    const y = (x % 7) % 6;
    if (y === 0) return 0;
    const z = x % 14;
    return z > 5
        ? z - 2
        : z;
};

day(0); // 0
day(1); // 1
day(2); // 2
day(3); // 3
day(4); // 4
day(5); // 5
day(6); // 0
day(7); // 0
day(8); // 6
day(9); // 7
day(10); // 8
day(11); // 9
day(12); // 10
day(13); // 0
day(14); // 0
day(15); // 1
// ...
Paul S.
  • 64,864
  • 9
  • 122
  • 138
  • Thank you. Let me test it out. – DarkRunner May 12 '18 at 14:47
  • `%` is not a modulo or mod operator, it is a remainder operator. It works like modulo for positive numbers, but not negative numbers. – RobG May 13 '18 at 09:05
  • @RobG you're right, my description of what the code does is assuming positive numbers (and therefore equality). If negative numbers need to be accepted too then you'd need something like `const mod = (x, base) => {const y = x % base; return y < 0 ? y + base : y;}` (assuming positive base) – Paul S. May 13 '18 at 14:06
  • 1
    @PaulS.—it can be a [*bit simpler*](https://stackoverflow.com/questions/4467539/javascript-modulo-gives-a-negative-result-for-negative-numbers) than that. ;-) – RobG May 14 '18 at 09:48
  • @RobG a lot more simple indeed! Looks good (other than modifying a built-in's prototype, which is not recommended by TC39, cf. MooTools) – Paul S. May 15 '18 at 21:45
  • @PaulS.—yes, agree on not modifying built-ins but the algorithm is sound. ;-) – RobG May 16 '18 at 08:26