skip to Main Content

I am trying to restrict users from seeing certain information on a landing page to only page by comparing if todays date is after Tuesday at 6pm within the current week.

I have been trying to setup this condition but Date/Time functions aren’t my forte.

Using the below I am able to determine the days of the week, but it seems a little buggy in that if within a calendar week a new month starts, the logic resets to the next/previous month.

const today = new Date("2022-11-03 16:20:04");
const first = today.getDate() - today.getDay() + 1;
const tuesday = new Date(today.setDate(first + 1));
const wednesday = new Date(today.setDate(first + 2));
const thursday = new Date(today.setDate(first + 3));
const friday = new Date(today.setDate(first + 4));

console.log('tuesday: ' + tuesday);

const diffTime = Math.abs(tuesday - today);
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); 

console.log(diffDays + " days");

I figured by determining by how many days from monday, I could determine if this had been surpassed or not. Unfortunately, this also doesn’t take into account time, only date as well.

3

Answers


  1. The problem with the code you have is that setDate modifies the Date object on which you call it, so all those calls to today.setDate are modifying the today date. To fix the existing code, first copy today and then modify the copied object:

    const today = new Date("2022-11-03 16:20:04");
    const first = today.getDate() - today.getDay() + 1;
    const tuesday = new Date(today);    // Copy today...
    tuesday.setDate(first + 1);         // ...now modify
    const wednesday = new Date(today);  // Copy today...
    wednesday.setDate(first + 2);       // ..now modify
    const thursday = new Date(today);   // ...
    thursday.setDate(first + 3);
    const friday = new Date(today);
    friday.setDate(first + 4);
    
    console.log("tuesday: " + tuesday);
    
    const diffTime = Math.abs(tuesday - today);
    const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
    
    console.log(diffDays + " days");

    (Side note: It didn’t used to be reliable to copy Date objects as shown above. If you need to support slightly out-of-date browsers, add + prior to today so you have new Date(+today).)

    But it seems like there’s a simpler way to check if a given date is after 6 p.m. on the current week’s Tuesday:

    function currentTuesdayAt6PM() {
        const today = new Date();
        const first = today.getDate() - today.getDay() + 1;
        const tuesday = new Date(today); // or `= new Date(+today);`
        tuesday.setDate(first + 1);
        tuesday.setHours(18, 0, 0, 0);
        return tuesday;
    }
    
    function isAfterCurrentTuesday(dt) {
        // Get the Tuesday for the current week at 6 p.m.
        const tuesday = currentTuesdayAt6PM();
    
        // Check date vs. Tuesday at 6 p.m.
        return dt > tuesday;
    }
    

    Live Example:

    function currentTuesdayAt6PM() {
        const today = new Date();
        const first = today.getDate() - today.getDay() + 1;
        const tuesday = new Date(today); // or `= new Date(+today);`
        tuesday.setDate(first + 1);
        tuesday.setHours(18, 0, 0, 0);
        return tuesday;
    }
    
    function isAfterCurrentTuesday(dt) {
        // Get the Tuesday for the current week at 6 p.m.
        const tuesday = currentTuesdayAt6PM();
    
        // Check date vs. Tuesday at 6 p.m.
        return dt > tuesday;
    }
    
    function test(dt) {
        const result = isAfterCurrentTuesday(dt);
        const resultText = result ? "is >" : "is not >";
        console.log(`${dt.toLocaleString()} ${resultText} ${currentTuesdayAt6PM().toLocaleString()}`);
    }
    
    test(new Date("2022-11-03 16:20:04")); // After
    test(new Date("2022-11-01 16:20:04")); // Before
    test(new Date("2022-11-01 18:00:00")); // Before (we're doing >, not >=)
    test(new Date("2022-11-01 18:20:04")); // After
    test(new Date("2022-10-31 18:20:04")); // Before
    Login or Signup to reply.
  2. getDay() on Date gives number of day in a week starting 0(Sunday), 1(Monday), 2 (Tuesday).
    getHours() gives hours.
    Making sure we crossed 18hrs which is 6pm when day is 2 or day > 2.

    const today = new Date();
    const showInfo = (today.getDay() > 2  || today.getDay() === 2 && today.getHours() > 18)
    
    Login or Signup to reply.
  3. If Monday is the first day of the week, a general function to return a particular time on a particular day of the same week as the supplied date can help.

    Then just compare the returned date to "now".

    function getDayAt(date = new Date(), day = 1, hour = 6) {
      let d = new Date(date);
      d.setDate(d.getDate() - (d.getDay() || 7) + day);
      d.setHours(hour, 0, 0, 0);
      return d;
    }
    
    // Is now before Tuesday at 6 pm?
    let now = new Date();
    console.log('Now is before 6 pm Tuesday: ' + 
                 (now < getDayAt(now, 2, 18)));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search