检查同一周是否有两个日期.(在JS中)



例如。输入1:

date 1='2021-09-20'
date 2='2021-09-24'

输出1:

Present in same week

输入2:

date 1='2021-09-24'
date 2='2021-09-27'

输出1:

Not in same week

请用javascript提供解决方案。

const getWeek = (date) => {
const janFirst = new Date(date.getFullYear(), 0, 1);
// Source: https://stackoverflow.com/a/27125580/3307678
return Math.ceil((((date.getTime() - janFirst.getTime()) / 86400000) + janFirst.getDay() + 1) / 7);
}
const isSameWeek = (dateA, dateB) => {
return getWeek(dateA) === getWeek(dateB);
}
const dateA = new Date('2021-09-20')
const dateB = new Date('2021-09-24')
const dateC = new Date('2021-09-27')
console.log(isSameWeek(dateA, dateB));
console.log(isSameWeek(dateA, dateC));

一种momentjs单线性:

moment().format('W')

相关内容

  • 没有找到相关文章

最新更新