我如何在JavaScript中计算出两个Date()对象的差异,而只返回差异中的月份数?
任何帮助都是最好的:)
我如何在JavaScript中计算出两个Date()对象的差异,而只返回差异中的月份数?
任何帮助都是最好的:)
当前回答
当日期和时间无关紧要的月份数
在这种情况下,我不关心完整的月份,部分月份,一个月有多长等等。我只需要知道月数。一个相关的现实案例是每个月都有一份报告,我需要知道应该有多少份报告。
例子:
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
其他回答
getMonthDiff(d1, d2) {
var year1 = dt1.getFullYear();
var year2 = dt2.getFullYear();
var month1 = dt1.getMonth();
var month2 = dt2.getMonth();
var day1 = dt1.getDate();
var day2 = dt2.getDate();
var months = month2 - month1;
var years = year2 -year1
days = day2 - day1;
if (days < 0) {
months -= 1;
}
if (months < 0) {
months += 12;
}
return months + years*!2;
}
下面的代码片段帮助我找到两个日期之间的月份
找到两个日期之间的月份计数JS
两个日期之间的月份 代码片段
function diff_months_count(startDate, endDate) {
var months;
var d1 = new Date(startDate);
var d2 = new Date(endDate);
months = (d2.getFullYear() - d1.getFullYear()) * 12;
months -= d1.getMonth();
months += d2.getMonth();
return months <= 0 ? 0 : months;
}
扩展一下@ 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),但它更准确,而且只适用于几行代码。
下面的逻辑将在几个月内取得差异
(endDate.getFullYear()*12+endDate.getMonth())-(startDate.getFullYear()*12+startDate.getMonth())
下面是一个函数,它精确地提供了两个日期之间的月数。 默认行为只计算整个月份,例如3个月和1天将导致3个月的差异。您可以通过将roundUpFractionalMonths参数设置为true来防止这种情况,因此3个月和1天的差异将返回为4个月。
上面公认的答案(T.J.克劳德的答案)是不准确的,它有时会返回错误的值。
例如,monthDiff(new Date('Jul 01, 2015'), new Date('Aug 05, 2015'))返回0,这显然是错误的。正确的差值是1个月或2个月。
这是我写的函数:
function getMonthsBetween(date1,date2,roundUpFractionalMonths)
{
//Months will be calculated between start and end dates.
//Make sure start date is less than end date.
//But remember if the difference should be negative.
var startDate=date1;
var endDate=date2;
var inverse=false;
if(date1>date2)
{
startDate=date2;
endDate=date1;
inverse=true;
}
//Calculate the differences between the start and end dates
var yearsDifference=endDate.getFullYear()-startDate.getFullYear();
var monthsDifference=endDate.getMonth()-startDate.getMonth();
var daysDifference=endDate.getDate()-startDate.getDate();
var monthCorrection=0;
//If roundUpFractionalMonths is true, check if an extra month needs to be added from rounding up.
//The difference is done by ceiling (round up), e.g. 3 months and 1 day will be 4 months.
if(roundUpFractionalMonths===true && daysDifference>0)
{
monthCorrection=1;
}
//If the day difference between the 2 months is negative, the last month is not a whole month.
else if(roundUpFractionalMonths!==true && daysDifference<0)
{
monthCorrection=-1;
}
return (inverse?-1:1)*(yearsDifference*12+monthsDifference+monthCorrection);
};