思路
1.分别计算两个日期所在周对应的周一的日期,比如2022-12-21,为周三,求对应的周一日期,则为2022-12-19,再把求得的两个日期进行天数相减,再除以7,即为两个日期相差的星期数。
原生date实现:
function getDifferWeeksBetweenTwoDates1(str1, str2) {
//1.转换为date对象
let date1 = new Date(str1);
let date2 = new Date(str2);
//2.由星期推算出该日所在周的星期一与该日相差天数
let diffDaysToMon1 = date1.getDay() - 1;
let diffDaysToMon2 = date2.getDay() - 1;
if(diffDaysToMon1 < 0){
diffDaysToMon1 = 6;
}
if(diffDaysToMon2 < 0){
diffDaysToMon2 = 6;
}
//3.获取参考计算日
let referDateDay1 = new Date(date1).setDate(date1.getDate() - diffDaysToMon1);
let referDateDay2 = new Date(date2).setDate(date2.getDate() - diffDaysToMon2);
let diffDays = parseInt(Math.abs(referDateDay1 - referDateDay2) / 1000 / 60 / 60 / 24);
//4.计算相差周数
let diffWeeks = diffDays / 7;
return diffWeeks;
}
moment实现:
function getDifferWeeksBetweenTwoDates2(str1,str2) {
//1.转换为moment对象
let moment1 = moment(str1);
let moment2 = moment(str2);
//2.由星期推算出该日所在周的星期一与该日相差天数
let diffDays1 = moment1.format("E") - 1;
let diffDays2 = moment2.format("E") - 1;
//3.获取参考计算日
let referMoment1 = moment1.subtract(diffDays1, "days");
let referMoment2 = moment2.subtract(diffDays2, "days");
//4.计算相差周数
let diffWeeks = Math.abs(Math.floor(referMoment1.diff(referMoment2, "days", true) / 7));
return diffWeeks;
}
2.设日期a为x1,与周一差为k1,日期b为x2,与周一差为k2,周一对应日期分别为y1,y2,它们相差天数为z。
则有 z = x2 - k2 - (x1 - k1) (x2>=x1) , z = -1 * ( x2 - k2 - (x1 - k1)) (x2 < x1)
function getDifferWeeksBetweenTwoDates3(str1, str2) {
//1.转换为date对象
let date1 = new Date(str1);
let date2 = new Date(str2);
//2.由星期推算出该日所在周的星期一与该日相差天数
let diffDaysToMon1 = date1.getDay() - 1;
let diffDaysToMon2 = date2.getDay() - 1;
if(diffDaysToMon1 < 0){
diffDaysToMon1 = 6;
}
if(diffDaysToMon2 < 0){
diffDaysToMon2 = 6;
}
let time1 = date1.getTime();
let time2 = date2.getTime();
let diffDays = parseInt((time1 - time2) / 1000 / 60 / 60 / 24);
let referMonDiffDays = (time1 >= time2 ? 1 : -1) * (diffDays - diffDaysToMon1 + diffDaysToMon2);
//4.计算相差周数
let diffWeeks =referMonDiffDays / 7;
return diffWeeks;
}
测试
const testCase = [
["2022-01-01","2022-01-01",0],
["2022-01-02","2022-01-04",1],
["2022-01-01","2021-12-11",3],
["2018-03-12","2015-04-12",153],
["2022-12-26","2023-01-03",1],
]
function testCaseFunc() {
testCase.forEach(dates=>{
const [startDate,endDate,result] = dates;
console.assert(getDifferWeeksBetweenTwoDates1(startDate,endDate) === result,"failed");
console.assert(getDifferWeeksBetweenTwoDates2(startDate,endDate) === result,"failed");
console.assert(getDifferWeeksBetweenTwoDates3(startDate,endDate) === result,"failed");
})
}
testCaseFunc();
性能比较
const testCase = [
["2022-01-01","2022-01-01",0],
["2022-01-02","2022-01-04",1],
["2022-01-01","2021-12-11",3],
["2018-03-12","2015-04-12",153],
["2022-12-26","2023-01-03",1],
]
const testPerformanceCount = 10000;
function testPerformanceFunc() {
console.time("getDifferWeeksBetweenTwoDates1");
for(let i=0;i<testCount;i++ ){
testCase.forEach(dates=>{
const [startDate,endDate] = dates;
getDifferWeeksBetweenTwoDates1(startDate,endDate);
})
}
console.timeEnd("getDifferWeeksBetweenTwoDates1");
console.time("getDifferWeeksBetweenTwoDates2");
for(let i=0;i<testCount;i++ ){
testCase.forEach(dates=>{
const [startDate,endDate] = dates;
getDifferWeeksBetweenTwoDates2(startDate,endDate);
})
}
console.timeEnd("getDifferWeeksBetweenTwoDates2");
console.time("getDifferWeeksBetweenTwoDates3");
for(let i=0;i<testCount;i++ ){
testCase.forEach(dates=>{
const [startDate,endDate] = dates;
getDifferWeeksBetweenTwoDates3(startDate,endDate);
})
}
console.timeEnd("getDifferWeeksBetweenTwoDates3");
}
testPerformanceFunc();
使用方法2的话不用先计算出两个日期对应的周一日期,性能较方法1快了2倍,如果使用moment的话,则快了20倍。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。