我如何在JavaScript中计算出两个Date()对象的差异,而只返回差异中的月份数?
任何帮助都是最好的:)
我如何在JavaScript中计算出两个Date()对象的差异,而只返回差异中的月份数?
任何帮助都是最好的:)
当前回答
如果你不考虑每月的日期,这是迄今为止最简单的解决方案
function monthDiff(dateTo, dateTo) 回来了 (12 * (dateTo.getFullYear) - dateyear .getFullYear() 的 /和 控制台(新日期(2000年,01年),新日期(2000年,02年)// 1 控制台,log(monthDiff, new Date(1999, 02), new Date(2000, 02)) // 12年 控制台.log(monthDiff,新日期(2009年,11年),新日期(2010年,0)// 1
注意,月份索引是基于0的。这意味着一月= 0,十二月= 11。
其他回答
你也可以考虑这个解决方案,这个函数返回整数或数字形式的月差
将开始日期作为第一个或最后一个参数传递是容错的。这意味着,函数仍然会返回相同的值。
const diffInMonths = (end, start) => { var timeDiff = Math.abs(end.getTime() - start.getTime()); 返回数学。round(timeDiff / (2e3 * 3600 * 365.25)); } const result = diffInMonths(new Date(2015, 3,28), new Date(2010, 1,25)); //显示月差值为整数/数字 console.log(结果);
有时你可能想要得到两个日期之间的月份数量,完全忽略日期部分。例如,如果你有两个日期——2013/06/21和2013/10/18——你只关心2013/06和2013/10部分,下面是场景和可能的解决方案:
var date1=new Date(2013,5,21);//Remember, months are 0 based in JS
var date2=new Date(2013,9,18);
var year1=date1.getFullYear();
var year2=date2.getFullYear();
var month1=date1.getMonth();
var month2=date2.getMonth();
if(month1===0){ //Have to take into account
month1++;
month2++;
}
var numberOfMonths;
1.如果您只想知道两个日期之间的月份数,不包括第1个月和第2个月
numberOfMonths = (year2 - year1) * 12 + (month2 - month1) - 1;
2.如果你想包括这两个月中的任何一个
numberOfMonths = (year2 - year1) * 12 + (month2 - month1);
3.如果你想包括这两个月
numberOfMonths = (year2 - year1) * 12 + (month2 - month1) + 1;
任何值连同它的绝对值一起返回。
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;
}
扩展一下@ 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),但它更准确,而且只适用于几行代码。
“月差数”的定义有很多不同的解释。: -)
您可以从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(抱歉。)