wordpress 用于检查当前日期是否在本周星期二下午6点之后的Javascript

jhdbpxl9  于 2022-11-02  发布在  WordPress
关注(0)|答案(4)|浏览(163)

我正试图限制用户看到的某些信息在一个登陆页面,只有通过比较,如果今天的日期是在周二下午6时后,在当前一周的页面。
我一直在尝试设置这个条件,但日期/时间功能不是我的强项。
使用下面的我能够确定一周中的几天,但它似乎有点bug,如果在一个日历周内一个新的月开始,逻辑重置为下一个/前一个月。

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");

我想通过确定从星期一算起的天数,我可以确定这个数字是否已经被超过。不幸的是,这也没有考虑时间,只有日期。

jk9hmnmh

jk9hmnmh1#

您只需要检查日期的星期是〉2(星期二)还是等于2,小时是〉= 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))
brjng4g3

brjng4g32#

您的程式码问题在于setDate会修改您呼叫它的Date对象,因此所有对today.setDate的呼叫都会修改today日期。若要修正现有的程式码,请先复制today,然后修改复制的对象:

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");
  • (附注:以前复制Date对象是不可靠的,如果你需要支持稍微过时的浏览器,在today之前添加+,这样你就有了new Date(+today)。)*

但似乎有一种更简单的方法来检查给定日期是否在本周星期二下午6点之后:

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 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
xv8emn3q

xv8emn3q3#

getDay()在Date上给出一周中的天数,从0(星期日)、1(星期一)、2(星期二)开始。getHours()给出小时数。确保我们越过了18小时,也就是当天是2或天〉2时的下午6点。

const today = new Date();
const showInfo = (today.getDay() > 2  || today.getDay() === 2 && today.getHours() > 18)
ocebsuys

ocebsuys4#

如果星期一是一周的第一天,那么使用一个通用函数来返回与提供的日期相同的一周中某一天的特定时间会很有帮助。
然后将返回的日期与“现在”进行比较。

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)));

相关问题