gpt4 book ai didi

javascript - Moment js 检查某个起始日期和截止日期是否在本周

转载 作者:行者123 更新时间:2023-12-02 17:28:03 25 4
gpt4 key购买 nike

我的对象有一个开始日期和结束日期,根据这两个日期,我需要检查当前周是否在该范围内,但我有点卡住了。

我创建了这个函数来使用 moment js 获取当前星期:

function getCurrentWeek() {
var currentDate = moment();

var weekStart = currentDate.clone().startOf('week');
var weekEnd = currentDate.clone().endOf('week');

var days = [];

for (var i = 0; i <= 6; i++) {
days.push(moment(weekStart).add(i, 'days').format("YYYY-MM-DD"));
}

return days;
}

这将返回本周的数组:

["2020-02-09", "2020-02-10", "2020-02-11", "2020-02-12", "2020-02-13", "2020-02-14", "2020-02-15"]

现在如何验证当前周是否属于起始日期和截止日期?

from: 2020-02-03
to: 2020-02-24

最佳答案

function isBetween(from, to){
let today = moment().format('YYYY-MM-DD')
let afterFromDate = moment(today).isSameOrAfter(from)
let beforeToDate = moment(today).isSameOrBefore(to)
if(afterFromDate && beforeToDate){
return true
}
return false
}
console.log(isBetween(from, to))

关于javascript - Moment js 检查某个起始日期和截止日期是否在本周,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60186936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com