提问人:designmode 提问时间:11/1/2022 更新时间:11/2/2022 访问量:136
Javascript 检查今天的日期是否在本周的星期二下午 6 点之后
Javascript to check if todays date is after Tuesday at 6pm in current week
问:
我试图通过比较今天的日期是否在本周内的星期二下午 6 点之后,将用户在登录页面上看到某些信息限制为仅页面。
我一直在尝试设置此条件,但日期/时间功能不是我的强项。
使用下面的内容,我能够确定一周中的几天,但似乎有点错误,因为如果在一个日历周内开始一个新的月份,逻辑会重置为下一个/上个月。
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");
我想通过确定从星期一开始的多少天,我可以确定是否已经超过了这个。不幸的是,这也没有考虑时间,只考虑日期。
答:
0赞
T.J. Crowder
11/1/2022
#1
你所拥有的代码的问题在于,它修改了你调用它的对象,所以所有这些调用都在修改日期。若要修复现有代码,请先复制并修改复制的对象: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
对象过去并不可靠。如果您需要支持稍微过时的浏览器,请在今天
之前添加 +
,以便您拥有新的 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
0赞
Thirunahari ManindraTeja
11/1/2022
#2
getDay() on Date 给出从 0(星期日)、1(星期一)、2(星期二)开始的一周中的天数。 getHours() 给出小时数。 确保我们越过了 18 小时,即下午 6 点,当第 2 天或第 2 天>。
const today = new Date();
const showInfo = (today.getDay() > 2 || today.getDay() === 2 && today.getHours() > 18)
0赞
RobG
11/2/2022
#3
如果星期一是一周的第一天,则在提供的日期返回同一周特定日期的特定时间的一般函数可能会有所帮助。
然后,只需将返回的日期与“现在”进行比较。
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)));
评论
today.getDate() - today.getDay() + 1;
today.getDate() - (today.getDay() || 7) + 1