我如何在JavaScript中计算出两个Date()对象的差异,而只返回差异中的月份数?
任何帮助都是最好的:)
我如何在JavaScript中计算出两个Date()对象的差异,而只返回差异中的月份数?
任何帮助都是最好的:)
当前回答
下面的代码还将部分月份中的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
其他回答
当日期和时间无关紧要的月份数
在这种情况下,我不关心完整的月份,部分月份,一个月有多长等等。我只需要知道月数。一个相关的现实案例是每个月都有一份报告,我需要知道应该有多少份报告。
例子:
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 monthDiff(d1, d2) {
var months;
months = (d2.getFullYear() - d1.getFullYear()) * 12;
months += d2.getMonth() - d1.getMonth();
return months;
}
下面是另一种更少循环的方法:
calculateTotalMonthsDifference = function(firstDate, secondDate) {
var fm = firstDate.getMonth();
var fy = firstDate.getFullYear();
var sm = secondDate.getMonth();
var sy = secondDate.getFullYear();
var months = Math.abs(((fy - sy) * 12) + fm - sm);
var firstBefore = firstDate > secondDate;
firstDate.setFullYear(sy);
firstDate.setMonth(sm);
firstBefore ? firstDate < secondDate ? months-- : "" : secondDate < firstDate ? months-- : "";
return months;
}
它还计算天数并以月为单位进行转换。
function monthDiff(d1, d2) {
var months;
months = (d2.getFullYear() - d1.getFullYear()) * 12; //calculates months between two years
months -= d1.getMonth() + 1;
months += d2.getMonth(); //calculates number of complete months between two months
day1 = 30-d1.getDate();
day2 = day1 + d2.getDate();
months += parseInt(day2/30); //calculates no of complete months lie between two dates
return months <= 0 ? 0 : months;
}
monthDiff(
new Date(2017, 8, 8), // Aug 8th, 2017 (d1)
new Date(2017, 12, 12) // Dec 12th, 2017 (d2)
);
//return value will be 4 months
function calcualteMonthYr(){
var fromDate =new Date($('#txtDurationFrom2').val()); //date picker (text fields)
var toDate = new Date($('#txtDurationTo2').val());
var months=0;
months = (toDate.getFullYear() - fromDate.getFullYear()) * 12;
months -= fromDate.getMonth();
months += toDate.getMonth();
if (toDate.getDate() < fromDate.getDate()){
months--;
}
$('#txtTimePeriod2').val(months);
}