我如何在JavaScript中计算出两个Date()对象的差异,而只返回差异中的月份数?
任何帮助都是最好的:)
我如何在JavaScript中计算出两个Date()对象的差异,而只返回差异中的月份数?
任何帮助都是最好的:)
“月差数”的定义有很多不同的解释。: -)
您可以从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(抱歉。)
我知道这真的很晚了,但还是把它贴出来,以防它能帮助到其他人。下面是我想出的一个函数,它似乎很好地计算了两个日期之间的月份差异。不可否认,这个方法比克劳德的方法要粗俗得多,但通过遍历日期对象提供了更准确的结果。它是在AS3中,但你应该能够放弃强类型,你会有JS。请随意让大家看起来更漂亮!
function countMonths ( startDate:Date, endDate:Date ):int
{
var stepDate:Date = new Date;
stepDate.time = startDate.time;
var monthCount:int;
while( stepDate.time <= endDate.time ) {
stepDate.month += 1;
monthCount += 1;
}
if ( stepDate != endDate ) {
monthCount -= 1;
}
return monthCount;
}
如果你不考虑每月的日期,这是迄今为止最简单的解决方案
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。
下面是另一种更少循环的方法:
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;
}
一种方法是编写一个使用JODA库的简单Java Web服务(REST/JSON)
http://joda-time.sourceforge.net/faq.html#datediff
计算两个日期之间的差异,并从javascript调用该服务。
这假设您的后端是Java。
有时你可能想要得到两个日期之间的月份数量,完全忽略日期部分。例如,如果你有两个日期——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;
如果你需要计算完整的月份,不管这个月是28、29、30还是31天。下面应该可以。
var months = to.getMonth() - from.getMonth()
+ (12 * (to.getFullYear() - from.getFullYear()));
if(to.getDate() < from.getDate()){
months--;
}
return months;
这是答案https://stackoverflow.com/a/4312956/1987208的扩展版本,但修复了从1月31日到2月1日(1天)计算1个月的情况。
这将包括以下内容;
1月1日至1月31日—> 30天—>将导致0(逻辑上,因为它不是一个完整的月) 2月1日至3月1日-> 28或29天->将导致1(逻辑上,因为它是一个完整的月) 2月15日至3月15日-> 28或29天->将导致1(逻辑上,因为一个月过去了) 1月31日至2月1日-> 1天->结果为0(明显,但在1个月后的结果中提到的答案)
JavaScript中两个日期的月份差异:
start_date = new Date(year, month, day); //Create start date object by passing appropiate argument
end_date = new Date(new Date(year, month, day)
从start_date到end_date的总月份:
total_months = (end_date.getFullYear() - start_date.getFullYear())*12 + (end_date.getMonth() - start_date.getMonth())
扩展一下@ 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),但它更准确,而且只适用于几行代码。
下面是一个函数,它精确地提供了两个日期之间的月数。 默认行为只计算整个月份,例如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);
};
有两种方法,数学的和快速的,但受制于日历的变幻莫测,或者迭代的和缓慢的,但处理所有奇怪的(或者至少委托处理它们到一个经过良好测试的库)。
If you iterate through the calendar, incrementing the start date by one month & seeing if we pass the end date. This delegates anomaly-handling to the built-in Date() classes, but could be slow IF you're doing this for a large number of dates. James' answer takes this approach. As much as I dislike the idea, I think this is the "safest" approach, and if you're only doing one calculation, the performance difference really is negligible. We tend to try to over-optimize tasks which will only be performed once.
现在,如果您在数据集中计算这个函数,您可能不希望在每一行上运行该函数(或者上帝禁止,每条记录多次)。在这种情况下,您几乎可以使用这里的任何其他答案,除了接受的答案,这是错误的(new Date()和new Date()之间的差异是-1)?
下面是我尝试的一种数学而快速的方法,它解释了不同的月份长度和闰年。你真的应该只使用这样的函数,如果你将应用它到一个数据集(做这个计算一遍又一遍)。如果只需要执行一次,可以使用上面James的迭代方法,因为您正在将所有(许多)异常的处理委托给Date()对象。
function diffInMonths(from, to){
var months = to.getMonth() - from.getMonth() + (12 * (to.getFullYear() - from.getFullYear()));
if(to.getDate() < from.getDate()){
var newFrom = new Date(to.getFullYear(),to.getMonth(),from.getDate());
if (to < newFrom && to.getMonth() == newFrom.getMonth() && to.getYear() %4 != 0){
months--;
}
}
return 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);
}
计算两个日期之间的差,包括月的分数(天)。
var difference = (date2.getDate() - date1.getDate()) / 30 +
date2.getMonth() - date1.getMonth() +
(12 * (date2.getFullYear() - date1.getFullYear()));
例如: 日期:24/09/2015(2015年9月24日) 日期2:09/11/2015(2015年11月9日) 差异:2.5个月
以月为单位考虑每个日期,然后相减找出差值。
var past_date = new Date('11/1/2014');
var current_date = new Date();
var difference = (current_date.getFullYear()*12 + current_date.getMonth()) - (past_date.getFullYear()*12 + past_date.getMonth());
这将得到两个日期之间的月差,忽略日。
anyVar = (((DisplayTo.getFullYear() * 12) + DisplayTo.getMonth()) - ((DisplayFrom.getFullYear() * 12) + DisplayFrom.getMonth()));
下面的代码还将部分月份中的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(d1, d2) {
var months;
months = (d2.getFullYear() - d1.getFullYear()) * 12;
months += d2.getMonth() - d1.getMonth();
return months;
}
function monthDiff(d1, d2) {
var months, d1day, d2day, d1new, d2new, diffdate,d2month,d2year,d1maxday,d2maxday;
months = (d2.getFullYear() - d1.getFullYear()) * 12;
months -= d1.getMonth() + 1;
months += d2.getMonth();
months = (months <= 0 ? 0 : months);
d1day = d1.getDate();
d2day = d2.getDate();
if(d1day > d2day)
{
d2month = d2.getMonth();
d2year = d2.getFullYear();
d1new = new Date(d2year, d2month-1, d1day,0,0,0,0);
var timeDiff = Math.abs(d2.getTime() - d1new.getTime());
diffdate = Math.abs(Math.ceil(timeDiff / (1000 * 3600 * 24)));
d1new = new Date(d2year, d2month, 1,0,0,0,0);
d1new.setDate(d1new.getDate()-1);
d1maxday = d1new.getDate();
months += diffdate / d1maxday;
}
else
{
if(!(d1.getMonth() == d2.getMonth() && d1.getFullYear() == d2.getFullYear()))
{
months += 1;
}
diffdate = d2day - d1day + 1;
d2month = d2.getMonth();
d2year = d2.getFullYear();
d2new = new Date(d2year, d2month + 1, 1, 0, 0, 0, 0);
d2new.setDate(d2new.getDate()-1);
d2maxday = d2new.getDate();
months += diffdate / d2maxday;
}
return months;
}
看看我用了什么:
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;
}
下面的逻辑将在几个月内取得差异
(endDate.getFullYear()*12+endDate.getMonth())-(startDate.getFullYear()*12+startDate.getMonth())
它还计算天数并以月为单位进行转换。
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 monthDiff(date1, date2, countDays) {
countDays = (typeof countDays !== 'undefined') ? countDays : false;
if (!date1 || !date2) {
return 0;
}
let bigDate = date1;
let smallDate = date2;
if (date1 < date2) {
bigDate = date2;
smallDate = date1;
}
let monthsCount = (bigDate.getFullYear() - smallDate.getFullYear()) * 12 + (bigDate.getMonth() - smallDate.getMonth());
if (countDays && bigDate.getDate() < smallDate.getDate()) {
--monthsCount;
}
return monthsCount;
}
你也可以考虑这个解决方案,这个函数返回整数或数字形式的月差
将开始日期作为第一个或最后一个参数传递是容错的。这意味着,函数仍然会返回相同的值。
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(结果);
当日期和时间无关紧要的月份数
在这种情况下,我不关心完整的月份,部分月份,一个月有多长等等。我只需要知道月数。一个相关的现实案例是每个月都有一份报告,我需要知道应该有多少份报告。
例子:
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;
}
任何值连同它的绝对值一起返回。
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;
}
这是我能找到的最简单的解。这将直接返回月数。尽管,它总是给出一个绝对值。
new Date(new Date(d2) - new Date(d1)).getMonth();
对于非绝对值,您可以使用以下解决方案:
function diff_months(startDate, endDate) {
let diff = new Date( new Date(endDate) - new Date(startDate) ).getMonth();
return endDate >= startDate ? diff : -diff;
}
下面的代码片段帮助我找到两个日期之间的月份
找到两个日期之间的月份计数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;
}
#这是我写的一段很好的代码,用于获取天数和月份 从给定日期开始
把你的手
/** * Date a end day * Date b start day * @param DateA Date @param DateB Date * @returns Date difference */ function getDateDifference(dateA, DateB, type = 'month') { const END_DAY = new Date(dateA) const START_DAY = new Date(DateB) let calculatedDateBy let returnDateDiff if (type === 'month') { const startMonth = START_DAY.getMonth() const endMonth = END_DAY.getMonth() calculatedDateBy = startMonth - endMonth returnDateDiff = Math.abs( calculatedDateBy + 12 * (START_DAY.getFullYear() - END_DAY.getFullYear()) ) } else { calculatedDateBy = Math.abs(START_DAY - END_DAY) returnDateDiff = Math.ceil(calculatedDateBy / (1000 * 60 * 60 * 24)) } const out = document.getElementById('output') out.innerText = returnDateDiff return returnDateDiff } // Gets number of days from given dates /* getDateDifference('2022-03-31','2022-04-08','day') */ // Get number of months from given dates getDateDifference('2021-12-02','2022-04-08','month') <div id="output"> </div>