TotalStartDate:01/28 TotalEndDate:02/22
我有一系列的日期,例如
数据:
StartDate2:01/27 endDate2:02/02
StartDate2:02/03 endDate2:02/09
StartDate2:02/10 endDate2:02/16
StartDate2:02/17 endDate2:02/23
dateRanges=[
{"startDate":01/27,"EndDate":02/02}
{"startDate":02/03,"EndDate":02/09}
{"startDate":02/10,"EndDate":02/16}
{"startDate":02/17,"EndDate":02/23}
]
现在我不需要再有天数符合初始日期范围内的数据范围
[0]索引数据范围从01/27开始,TotalStartDate从01/28开始,因此浪费了1天,并且只有6天可以作为计数
[1]索引数据范围从02/03开始,TotalStartDate从01/28开始。[1]索引数据范围的所有日期都位于TotalStartDate和TotalEndDate内,因此计数应为7
[2]数7
[3]将6作为结束日期大于totalEndDate。
答案 0 :(得分:2)
这是您要找的东西吗?
我们可以用来实现这一目标的算法:
下面是相同的代码。
我有dateRanges数组,inputStartDate,inputEnddate和我的逻辑来计算输入范围之间的天数,如下所示:
let dateRanges=[
{"startDate":"01/27","EndDate":"02/02"},
{"startDate":"02/03","EndDate":"02/09"},
{"startDate":"02/10","EndDate":"02/16"},
{"startDate":"02/17","EndDate":"02/23"}
]
let inputStartDate = "01/28"
let inputEndDate = "02/22"
dateRanges.forEach(eachDate => {
var endDateToConsider = moment(eachDate.EndDate+'/2019').isAfter(inputEndDate+"/2019") ? inputEndDate: eachDate.EndDate;
var startDateToConsider = moment(eachDate.startDate+'/2019').isAfter(inputStartDate+"/2019") ? eachDate.startDate : inputStartDate
var daysDifference = moment(endDateToConsider+'/2019').diff(moment(startDateToConsider+'/2019'), 'days') + 1
console.log(daysDifference)
})