gpt4 book ai didi

javascript - 将结束日期设置为最多 31 天

转载 作者:搜寻专家 更新时间:2023-10-30 22:03:22 24 4
gpt4 key购买 nike

我正在尝试从给定的 Start DateEnd Date 中提取周数。

如果日期范围超过 31 天,我需要将结束日期限制为最多 31 天。

示例:

Start date : 1st May 2018
default end date: 30th June 2018
New endDate should be: 31st May 2018

下面是我提取周数的代码

weekByDates() {
let dates = JSON.parse(localStorage.getItem('pfsessionFilter'));
let stDate = moment((dates[0].value)).format('YYYY-MM-DD');
let eDate = moment((dates[1].value)).format('YYYY-MM-DD');

let startDate = moment(stDate);
let endDate = moment(eDate);
let weekData = [];
if(endDate.diff(startDate, 'days')>31)
endDate = moment(startDate.add(31, 'days'));
while(startDate.isSameOrBefore(endDate)) {
if(weekData.length > 0) {
// Update end date
let lastObj = weekData[weekData.length - 1];
lastObj['endDate'] = moment(startDate).format('MM/DD');
lastObj['label'] = `${lastObj.startDate} - ${lastObj['endDate']} (week${weekData.length})`
startDate.add(1, 'days');
}
weekData.push({startDate: moment(startDate).format('MM/DD')});
startDate.add(6, 'days');
}
if(startDate.isAfter(endDate)) {
// Update last object
let lastObj = weekData[weekData.length - 1];
lastObj['endDate'] = moment(endDate).format('MM/DD');
lastObj['label'] = `${lastObj.startDate} - ${lastObj['endDate']} (week${weekData.length})`
}
console.log(weekData)
return weekData;
}

请帮忙说明如何将结束日期限制为从开始日期算起的 31 天?

https://plnkr.co/edit/6rU39T0vIPVQkFnydKbC?p=preview

最佳答案

检查范围是否大于 31 天,然后使用 add 方法作为

weekByDates() 
{
let dates = JSON.parse(localStorage.getItem('getDates'));
let stDate = moment((dates[0].value)).format('YYYY-MM-DD');
let eDate = moment((dates[1].value)).format('YYYY-MM-DD');
let startDate = moment(stDate);
let endDate = moment(eDate);
if(eDate.diff(stDate, 'days')>31)
endDate = moment(stDate).add(31, 'days');
let weekData = []; while(startDate.isSameOrBefore(endDate))
{ .... }

关于javascript - 将结束日期设置为最多 31 天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49894557/

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