0

I've been able to show/hide a button if the time is within the range, however, how could I modify this to also check if the date is every second Tuesday, starting on December 1st?

<p id="newButton">LIVE</p>

window.addEventListener("load", function(){
var newButton = document.getElementById("newButton");
const start = 12 * 60 + 30;
const end =  13 * 60 + 30;
const date = new Date(); 
const now = date.getHours() * 60 + date.getMinutes();

if(start <= now && now <= end) {
 newButton.style.display = "block";
 alert("in time");
}
else {
 newButton.style.display = "none";
 alert("offline");
}
}, false);
MacGyver_97
  • 229
  • 3
  • 14
  • 1
    I think this would do it: `date.getDay() == 2 && date.getDate() > 7 && date.getDate() < 15` – Scott Sauyet Nov 16 '20 at 20:40
  • I'm sorry, I don't follow how to integrate this into the rest. I'd need to check if the date is Dec 1, 15, 29, etc, and the time is between 12:30-1:30pm, and only then display the LIVE text. – MacGyver_97 Nov 16 '20 at 21:17
  • 1
    You could integrate it in various ways. One possibility: `const isSecondTuesday = date.getDay() == 2 && date.getDate() > 7 && date.getDate() < 15`, then extend `if (start <= now && now <= end) { ...}` to include it: `if (start <= now && now <= end && isSecondTuesday) {...}`. – Scott Sauyet Nov 16 '20 at 21:28
  • 1
    Fantastic, thank you. I think it works. :) – MacGyver_97 Nov 16 '20 at 21:30
  • Sorry, this may actually not be the correct answer. The statement checks if the day is Tuesday, but also only if the dates are between the 7th and the 15th of the month. Am I reading that right? The dates of every second Tuesday starting from Dec 1 likely doesn’t fall into that range, ie, Dec 29, Jan 26th, etc. – MacGyver_97 Nov 16 '20 at 23:35
  • Ah, I was answering a different question: the second Tuesday of December, the second Tuesday of January, the second Tuesday of March, etc. An answer for "every other Tuesday" would be different. I would probably round off the quotient of the difference between the test date and your target date divided by the number of milliseconds in a day, and then see if that has a remainder of `0` when divided by `14`, something like `const check = (date) => Math .round (date - 1606798800000) % 14 === 0` – Scott Sauyet Nov 17 '20 at 13:51

1 Answers1

1

Here's what I suggested in the comments:

const msPerDay = 24 * 60 * 60 * 1000;

window.addEventListener("load", function(){
  var newButton = document.getElementById("newButton");
  const start = 12 * 60 + 30;
  const end =  13 * 60 + 30;
  const date = new Date(); 
  const now = date.getHours() * 60 + date.getMinutes();

  if(start <= now && now <= end 
     && Math.round((date - 1606798800000) / msPerDay) % 14 === 0) {
    newButton.style.display = "block";
    alert("in time");
  }
  else {
    newButton.style.display = "none";
    alert("offline");
  }
}, false);

Note the interesting ambiguity in the question. "Check if the date is every second Tuesday, starting on December 1st?" can be read -- as intended -- to represent alternating Tuesdays. But my initial misreading, "the second Tuesday of any month" is perfectly understandable, if, I suppose, slightly less likely given that the noted start date is itself a Tuesday.

1606798800000 is simply the result of new Date('2020-12-01').getTime(). It's probably a silly optimization not to simply include something like that in the code. So an alternative version might be

  if (start <= now && now <= end &&  
      Math.round((date - new Date(2020, 11, 1).getTime()) / msPerDay) % 14 === 0)
Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103
  • What is `1606798800000`? – Cal Irvine Nov 17 '20 at 14:34
  • @CalIrvine: Sorry, updated. That was discussed in the comments to the question, but should have been included here. – Scott Sauyet Nov 17 '20 at 14:55
  • Thank you! Any suggestions on how to test this without actually waiting for December 1st, 15, 29, etc? Since the date relies on a server time that I don't have control over. – MacGyver_97 Nov 17 '20 at 15:49
  • You could change the hard-coded date to one two weeks ago, eight weeks ago, one hundred weeks ago. That should add some assurance. And double check that it doesn't fire when you do three or seven weeks ago, or some non-Tuesday. – Scott Sauyet Nov 17 '20 at 15:57
  • BTW, when you wrote "date - new Date(2020, 11, 1)", is that intentional, or did you mean "date - new Date(2020, 12, 1)"? – MacGyver_97 Nov 17 '20 at 16:22
  • 1
    Unfortunately, [I meant it](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date#:~:text=monthIndex). The JS Date API is terrible. But the second parameter to the constructor is the month *index* not the usual month *number*. This doesn't apply to the year or date (or any other fields.) – Scott Sauyet Nov 17 '20 at 17:14
  • 1
    Ah, again, thank you. So January is "0", and so forth. – MacGyver_97 Nov 17 '20 at 17:39
  • I just tried to modify the date to work for today, since it just happens to be the second Tuesday after November 3. I used this: "new Date(2020, 10, 3).getTime()) % 14", meaning the date should have calculated to November 17th. The time that I tested this is between 12:30 and 1:30pm, however it doesn't seem to work. Any idea? – MacGyver_97 Nov 17 '20 at 17:47
  • Did you really use "new Date(2020, 10, **3**)"? It presumably should be "new Date(2020, 10, **17**)". – Scott Sauyet Nov 17 '20 at 18:41
  • Yes, but wouldn't "new Date(2020,10,3).getTime()) % 14 === 0)" equal November 17 and every second Tuesday following? Otherwise, how does "new Date(2020, 11, 1).getTime()) % 14 === 0)" equal Dec 1, 15, 29, Jan 12, 26, and so on...? – MacGyver_97 Nov 17 '20 at 19:01
  • 1
    Yes, there was something missing, dividing by the length of a day. Sorry about that. – Scott Sauyet Nov 17 '20 at 20:52
  • Hello -- just to let you know, that unfortunately on this actual day, Dec 1, at 12:30pm, this script didn't work. Any idea? – MacGyver_97 Dec 01 '20 at 18:08
  • 1
    I'm sure it has to do with timezones and local versus GMT dates, but I don't have time to investigate. This answer may help: https://stackoverflow.com/a/15289883/1243641. The JS Date API is an unholy mess. Best of luck. – Scott Sauyet Dec 01 '20 at 18:22
  • I noticed that if I set the condition to be (2020, 11, 2) then the script works. – MacGyver_97 Dec 01 '20 at 19:03