两个日期之间的几个月差异 JavaScript 年

我将如何计算两个对象的差异 Date// 在 JavaScript, 并只返回差异的月数?

任何帮助都将是非常的 :/
已邀请:

江南孤鹜

赞同来自:

定义 "the number of months in the difference" 受到大量的解释。 :-/

您可以从对象中获得一年,月份和日期 JavaScript date. 根据您要查找的信息,您可以使用它们来了解两段时间之间的几个月。

例如, off-the-cuff, 它发现了多少

满月

在两个日期之间,不计算部分月 /例如,不包括每个日期的月份/:


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

monthDiff/
new Date/2008, 10, 4/, // November 4th, 2008
new Date/2010, 2, 12/ // March 12th, 2010
/;
// Result: 15: December 2008, all of 2009, and Jan & Feb 2010

monthDiff/
new Date/2010, 0, 1/, // January 1st, 2010
new Date/2010, 2, 12/ // March 12th, 2010
/;
// Result: 1: February 2010 is the only full month between them

monthDiff/
new Date/2010, 1, 1/, // February 1st, 2010
new Date/2010, 2, 12/ // March 12th, 2010
/;
// Result: 0: There are no *full* months between them


/请注意,该月的值 JavaScript 开始S. 0 = 一月。/

在上述内容中包含小数月份更复杂,因为本月的典型2月份的三天都是如此 /~10.714%/, 八月三天 /~9.677%/,, 当然,即使是2月也是一个移动的目标,具体取决于他是否是闰年。

还有一些
http://www.datejs.com/
, 可以用来 JavaScript, 这可能有助于这项任务。

君笑尘

赞同来自:

如果您没有考虑到一个月的日子,那么这是一个更简单的决定。


function monthDiff/dateFrom, dateTo/ {
return dateTo.getMonth// - dateFrom.getMonth// +
/12 * /dateTo.getFullYear// - dateFrom.getFullYear////
}


//examples
console.log/monthDiff/new Date/2000, 01/, new Date/2000, 02/// // 1
console.log/monthDiff/new Date/1999, 02/, new Date/2000, 02/// // 12 full year
console.log/monthDiff/new Date/2009, 11/, new Date/2010, 0/// // 1



请记住,本月的指数是基于 0. 代表着
January = 0


December = 11

.

石油百科

赞同来自:

有时您只能获得两个日期之间的月份,完全忽略白天部分。 因此,例如,如果你有两天 - 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.If 您只需要两个日期之间的月数,不包括和 month1, 和 month2


numberOfMonths = /year2 - year1/ * 12 + /month2 - month1/ - 1;


2.If 你想包括任何月份


numberOfMonths = /year2 - year1/ * 12 + /month2 - month1/;


3.If 你想包括两个月


numberOfMonths = /year2 - year1/ * 12 + /month2 - month1/ + 1;

小明明

赞同来自:

以下是准确定义两个日期之间的月数的函数。

例如,默认行为仅考虑了数月,例如, 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/;
};

龙天

赞同来自:

如果您需要考虑满月,无论哪个月会 28, 29, 30 或者 31 天。 以下应该工作。


var months = to.getMonth// - from.getMonth// 
+ /12 * /to.getFullYear// - from.getFullYear////;

if/to.getDate// < from.getDate///{
months--;
}
return months;


这是一个扩展的答案版本。

, 但是在计算时纠正这种情况 1 案件的月份 31 1月P. 1 二月 /1 天/.

它将涵盖以下内容;

从 1 1月P. 31 一月 - - - > 30 天 - - - > 将导致 0 /逻辑以来它不是整整一个月/

1 2月in. 1 玛莎 - - - > 28 或者 29 天 - - - > 将导致 1 /逻辑以来,这是一个完整的月份/

15 2月in. 15 玛莎 - - - > 28 或者 29 天 - - - > 将导致 1 /逻辑因为一个月过去了/

31 1月P. 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///

龙天

赞同来自:

我知道它真的很晚,但我仍然只发送它,如果它有助于他人。 这是我想出的一个函数,这似乎是在两个日期之间计算数月之间的差异很好。 据招待,他比粗鲁 Mr.Crowder's, 但通过通过日期对象提供更准确的结果。 他在 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;
}

窦买办

赞同来自:

扩展答案 @T.J.'s, 如果您正在寻找简单的月份,而不是完整的日历月,您只需检查日期是 d2 更多或平等的日期 d1. 那是,如果 d2 在他的一个月之后 d1 在她的月份,那就是 1 一个月更多。 所以你应该能够做到这一点:


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 Marta B. 4:00pm 和 3 四月 3:00pm/, 但它更准确,只有几行代码。

八刀丁二

赞同来自:

从几个月的角度考虑每个日期,然后扣除差异。


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///;


这将为您提供两个日期之间的几个月,忽略日期。

二哥

赞同来自:

有两种方法:数学 & 快速,但易受受损的日历,或迭代 & 慢但夹子与所有古怪 /或者至少委派了他们经过精心验证的图书馆的处理/.

如果您遍历日历,请增加一个月的开始日期 &, 看看我们是否经过最后日期。 这使得将异常的处理处理到嵌入式类别。 Date//, 但可能很慢

IF

你做了大量的日期。 詹姆斯的答案是基于这种方法。 我怎么喜欢这个想法,我认为这是一种方法 "safest", 如果你只做



计算,性能差异真的很微不足道。 我们倾向于尝试过度优化只能执行一次的任务。

现在,

如果一个

您在数据集中计算此功能,您可能不想在每行中运行此函数 /或者,上帝禁止,在记录中几次/. 在这种情况下,您可以在此处使用几乎任何其他答案。

除了

被接受的答案是错误的 /差差
new Date//


new Date//

平等的 -1/?

这是我对方法的打击 mathematical-and-quick, 这考虑了本月和闰年的不同长度。 如果您将其应用于数据集时,您真的必须使用类似的函数。 /让这个计算更多 & 时间/. 如果您只需要执行此操作,请使用上面的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;
}

江南孤鹜

赞同来自:

http://ggomeze.com/2011/12/07/ ... cript
你和另一种循环少的方法:


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

在整数或数字术语之间返回几个月之间的差异

播送

开始的日期

作为第一个或最后一个
param

这是一个容错。 这意味着该函数仍将返回相同的值。


const diffInMonths = /end, start/ => {
var timeDiff = Math.abs/end.getTime// - start.getTime///;
return Math.round/timeDiff / /2e3 * 3600 * 365//;
}

const result = diffInMonths/new Date/2015, 3, 28/, new Date/2010, 1, 25//;

// shows month difference as integer/number
console.log/result/;

快网

赞同来自:

计算两个日期之间的差异,包括本月的洛杉矶 /天/.


var difference = /date2.getDate// - date1.getDate/// / 30 +
date2.getMonth// - date1.getMonth// +
/12 * /date2.getFullYear// - date1.getFullYear////;


例如:

日期 1: 24/09/2015

/24 九月 2015/

日期 2: 09/11/2015

/9 十一月 2015/

区别: 2.5 /几个月/

冰洋

赞同来自:

它应该正常工作:


function monthDiff/d1, d2/ {
var months;
months = /d2.getFullYear// - d1.getFullYear/// * 12;
months += d2.getMonth// - d1.getMonth//;
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/;
}

八刀丁二

赞同来自:

以下代码返回两个日期之间的满月,也考虑到 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, 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;


}

八刀丁二

赞同来自:

低于逻辑将带来几个月的差异


/endDate.getFullYear//*12+endDate.getMonth///-/startDate.getFullYear//*12+startDate.getMonth///

奔跑吧少年

赞同来自:

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;
}

卫东

赞同来自:

看看我用了什么:


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;
}

窦买办

赞同来自:

他还认为日期并将它们转化为几个月。


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

小明明

赞同来自:

一天的月数 & 时间并不重要

在这种情况下,我对我不感兴趣的是整个月,不完整的几个月,月份持续多久,依此类推。我只需要知道月数。

现实世界中的相关案例是报告应该每月准备好,我需要知道应该有多少报告。

例子:

一月 = 1 月

一月 - 二月 = 2 月

十一月 - 一月 = 3 月

这是用于显示数字GOW的代码的详细示例。

让我们来 2 应该导致的时间标签 4 几个月

13 十一月 2019 当年 timestamp: 1573621200000

20 二月 2020 当年 timestamp: 1582261140000

也许有点不同于你的 timezone / 时间拉了。 日,几分钟和秒无关紧要,可以包括在内 timestamp, 但我们将忽略他们的实际计算。

步 1: 转换 timestamp 到 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


步 4: 拉月亮


15 - 11 = 4

; 我们得到了4个月的结果。

29 一个月的例子例子

十一月 2019 年3月 2022 一年是 29 几个月。 如果你把它们放在桌子里 excel, 你会看见 29 学期。

我们的开始月11

我们上个月 - 40
3 + /12 * /2022-2019// + 1


40 - 11 = 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;
}

詹大官人

赞同来自:

anyVar = ///DisplayTo.getFullYear// * 12/ + DisplayTo.getMonth/// - //DisplayFrom.getFullYear// * 12/ + DisplayFrom.getMonth////;

风见雨下

赞同来自:

一种方法会写一个简单 Java 网络服务 /REST/JSON/, 使用库 JODA

http://joda-time.sourceforge.net/faq.html#datediff
计算两个日期之间的差异并从中致电此服务 javascript.

它表明你的背部是 Java.

要回复问题请先登录注册