没有闰日的两个日期之间的差异

时间:2018-03-05 07:00:55

标签: javascript momentjs

我有开始日期和结束日期,我必须在这两个日期之间取得差异,但条件是,如果是闰年,我必须排除2月29日。我可以使用momentJS在两个日期之间获得差异。但如果它在那个日期范围内,如何排除第29个feb。

对于Ex:

//For Leap Year
var startDate = moment('2020-02-27')
var endDate = moment('2020-03-01')
console.log('Diff in leap year', endDate.diff(startDate, 'days')); //Result: 3



var startDate1 = moment('2018-02-27')
var endDate1 = moment('2018-03-01')
console.log('Diff in non leap year', endDate1.diff(startDate1, 'days')); //Result: 2 
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/moment.min.js"></script>

我期待跳跃和非闰年的统一结果,或者只是想从计算中排除第29个feb。差异可能超过一年。

有没有最佳解决方案呢?

4 个答案:

答案 0 :(得分:2)

您可以使用.isLeapYear()检查它是否是闰年,如果是,则从结果中减去一天:

function diffDates(startDate, endDate) {
  var diff = endDate.diff(startDate, 'days');

  while(startDate < endDate) {
    if(startDate.isLeapYear() && 
      moment(`${startDate.year()}-02-29`).isBetween(startDate, endDate)) {
      diff--;
    }
    startDate.year(startDate.year() + 1);
  }
  return diff;
}

var startDate = moment('2020-02-27')
var endDate = moment('2020-03-01')

console.log('Leap year days diff: ' + diffDates(startDate, endDate)); // 2

startDate = moment('2018-02-27')
endDate = moment('2018-03-01')
console.log('Non leap year days diff: ' + diffDates(startDate, endDate)); // 2

startDate = moment('2019-02-27')
endDate = moment('2020-03-01')
console.log('Between leap year days diff: ' + diffDates(startDate, endDate)); // 367

我添加了一个条款,以了解您的日期是否在使用.isBetween()的闰年之间的29日之间,并且每次创建while循环以从diff减去1 闰日在间隔内。

看到它正常工作here

答案 1 :(得分:1)

您可以使用startDate检查.isLeapYear()是否为闰年,然后相应地减去1。

&#13;
&#13;
function getDiff(startDate, endDate){
  var isLeapYear = startDate.isLeapYear();
  return endDate.diff(startDate, 'days') - (isLeapYear?1:0);
}


var startDate = moment('2020-02-27')
var endDate = moment('2020-03-01')
console.log('Diff in leap year', getDiff(startDate, endDate)); 

var startDate1 = moment('2018-02-27')
var endDate1 = moment('2018-03-01')
console.log('Diff in non leap year',getDiff(startDate1, endDate1));
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/moment.min.js"></script>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

使用getAnnotation(),您可以在日期范围之间迭代所有年份并获得预期结果

.isLeapYear()

答案 3 :(得分:0)

您可以尝试这样的事情:

  • 检查所选年份是否为闰年。
  • 如果是,请检查日期范围是否涵盖29th Feb
  • 如果是,则减1否则为0

function getDiff(startDate, endDate){
  var dayToExclude = moment('29/02/' + startDate.year(), 'DD/MM/YYYY');
  var excludeDays = startDate.isLeapYear() && startDate.isBefore(dayToExclude) && endDate.isAfter(dayToExclude) ? 1 : 0;
  return endDate.diff(startDate, 'days') - excludeDays;
}


var startDate = moment('2020-02-27')
var endDate = moment('2020-03-01')
console.log('Diff in leap year', getDiff(startDate, endDate)); 

var startDate1 = moment('2018-02-27')
var endDate1 = moment('2018-03-01')
console.log('Diff in non leap year',getDiff(startDate1, endDate1));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/moment.min.js"></script>