我如何在JavaScript中计算出两个Date()对象的差异,而只返回差异中的月份数?

任何帮助都是最好的:)


当前回答

扩展一下@ tj。的答案,如果你在寻找简单的月份,而不是完整的日历月份,你可以检查d2的日期是否大于或等于d1的日期。也就是说,如果d2的月份晚于d1的月份,那么就多了一个月。所以你应该可以这样做:

function monthDiff(d1, d2) {
    var months;
    months = (d2.getFullYear() - d1.getFullYear()) * 12;
    months -= d1.getMonth() + 1;
    months += d2.getMonth();
    // edit: increment months if d2 comes later in its month than d1 in its month
    if (d2.getDate() >= d1.getDate())
        months++
    // end edit
    return months <= 0 ? 0 : months;
}

monthDiff(
    new Date(2008, 10, 4), // November 4th, 2008
    new Date(2010, 2, 12)  // March 12th, 2010
);
// Result: 16; 4 Nov – 4 Dec '08, 4 Dec '08 – 4 Dec '09, 4 Dec '09 – 4 March '10

这并没有完全考虑到时间问题(例如,3月3日下午4:00和4月3日下午3:00),但它更准确,而且只适用于几行代码。

其他回答

下面的代码还将部分月份中的nr天考虑在内,从而返回两个日期之间的完整月份。

var monthDiff = function(d1, d2) {
  if( d2 < d1 ) { 
    var dTmp = d2;
    d2 = d1;
    d1 = dTmp;
  }

  var months = (d2.getFullYear() - d1.getFullYear()) * 12;
  months -= d1.getMonth() + 1;
  months += d2.getMonth();

  if( d1.getDate() <= d2.getDate() ) months += 1;

  return months;
}

monthDiff(new Date(2015, 01, 20), new Date(2015, 02, 20))
> 1

monthDiff(new Date(2015, 01, 20), new Date(2015, 02, 19))
> 0

monthDiff(new Date(2015, 01, 20), new Date(2015, 01, 22))
> 0

看看我用了什么:

function monthDiff() {
    var startdate = Date.parseExact($("#startingDate").val(), "dd/MM/yyyy");
    var enddate = Date.parseExact($("#endingDate").val(), "dd/MM/yyyy");
    var months = 0;
    while (startdate < enddate) {
        if (startdate.getMonth() === 1 && startdate.getDate() === 28) {
            months++;
            startdate.addMonths(1);
            startdate.addDays(2);
        } else {
            months++;
            startdate.addMonths(1);
        }
    }
    return months;
}

当日期和时间无关紧要的月份数

在这种情况下,我不关心完整的月份,部分月份,一个月有多长等等。我只需要知道月数。一个相关的现实案例是每个月都有一份报告,我需要知道应该有多少份报告。

例子:

1月= 1个月 一月到二月= 2个月 11月至1月= 3个月

这是一个详细的代码示例,以显示数字的走向。

让我们取2个时间戳,结果应该是4个月

2019年11月13日的时间戳:1573621200000 2020年2月20日的时间戳:1582261140000

可能与您的时区/时间略有不同。日、分和秒并不重要,可以包含在时间戳中,但在实际计算中我们将忽略它。

步骤1:将时间戳转换为JavaScript日期

let dateRangeStartConverted = new Date(1573621200000);
let dateRangeEndConverted = new Date(1582261140000);

步骤2:获取月份/年的整数值

let startingMonth = dateRangeStartConverted.getMonth();
let startingYear = dateRangeStartConverted.getFullYear();
let endingMonth = dateRangeEndConverted.getMonth();
let endingYear = dateRangeEndConverted.getFullYear();

这给了我们

开始月份:11 开始年份:2019年 结束月份:2 结束年份:2020年

步骤3:在月末加上(12 * (endYear - startYear)) + 1。

这使我们的开始月停留在11 这样月末就等于15 2 + (12 * (2020 - 2019))+ 1 = 15

第四步:减去月份

15 - 11 = 4;我们得到了4个月的结果。

29个月

2019年11月至2022年3月为29个月。如果你把这些输入到excel电子表格中,你会看到29行。

开始的月份是11 最后一个月是403 + (12 * (2022-2019))+ 1

40 minus 11 is 29

任何值连同它的绝对值一起返回。

function differenceInMonths(firstDate, secondDate) {
    if (firstDate > secondDate) [firstDate, secondDate] = [secondDate, firstDate];
    let diffMonths = (secondDate.getFullYear() - firstDate.getFullYear()) * 12;
    diffMonths -= firstDate.getMonth();
    diffMonths += secondDate.getMonth();
    return diffMonths;
}
 

“月差数”的定义有很多不同的解释。: -)

您可以从JavaScript date对象中获取年、月和月中的日期。根据您要查找的信息,您可以使用这些信息来计算出两个时间点之间有多少个月。

例如,off- cuff:

function monthDiff(d1, d2) {
    var months;
    months = (d2.getFullYear() - d1.getFullYear()) * 12;
    months -= d1.getMonth();
    months += d2.getMonth();
    return months <= 0 ? 0 : months;
}

function monthDiff(d1, d2) { var months; months = (d2.getFullYear() - d1.getFullYear()) * 12; months -= d1.getMonth(); months += d2.getMonth(); return months <= 0 ? 0 : months; } function test(d1, d2) { var diff = monthDiff(d1, d2); console.log( d1.toISOString().substring(0, 10), "to", d2.toISOString().substring(0, 10), ":", diff ); } test( new Date(2008, 10, 4), // November 4th, 2008 new Date(2010, 2, 12) // March 12th, 2010 ); // Result: 16 test( new Date(2010, 0, 1), // January 1st, 2010 new Date(2010, 2, 12) // March 12th, 2010 ); // Result: 2 test( new Date(2010, 1, 1), // February 1st, 2010 new Date(2010, 2, 12) // March 12th, 2010 ); // Result: 1

(请注意,JavaScript中的月份值以0 = January开始。)

包括上面的小数月份要复杂得多,因为典型的2月份的三天(~10.714%)比8月份的三天(~9.677%)占这个月的比例要大得多,当然,甚至2月份也是一个移动的目标,这取决于它是否是闰年。

还有一些可供JavaScript使用的日期和时间库,可能会使这类事情变得更容易。


注:上面曾经有一个+ 1,这里:

months = (d2.getFullYear() - d1.getFullYear()) * 12;
months -= d1.getMonth() + 1;
// −−−−−−−−−−−−−−−−−−−−^^^^
months += d2.getMonth();

因为一开始我说

...这将找出两个日期之间有多少个完整的月份,不包括部分月份(例如,不包括每个日期所在的月份)。

我删除它有两个原因:

结果发现,不计算部分月份并不是很多(大多数?)人想得到的答案,所以我认为我应该把它们分开。 即使按照这个定义,它也并不总是有效。: - d(抱歉。)