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.
CodePudding user response:
You just need to check whether the date's day of the week is > 2 (Tuesday) or that it is equal to 2 and the hour is >= 18:
const landingOK = date => date.getDay() > 2 || /* wednesday or after */
date.getDay() == 2 && date.getHours() >= 18
let date = new Date('2022-10-31 00:00')
console.log(date.toLocaleString(), landingOK(date))
date = new Date('2022-11-01 17:59')
console.log(date.toLocaleString(), landingOK(date))
date = new Date('2022-11-01 18:00')
console.log(date.toLocaleString(), landingOK(date))
date = new Date('2022-11-02 00:00')
console.log(date.toLocaleString(), landingOK(date))
CodePudding user response:
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
CodePudding user response:
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)