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
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;
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;
}
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/, 但它更准确,只有几行代码。
function calcualteMonthYr//{
var fromDate =new Date/$/'#txtDurationFrom2'/.val///; //date picker /text fields/
var toDate = new Date/$/'#txtDurationTo2'/.val///;
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
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//;
25 个回复
江南孤鹜
赞同来自:
您可以从对象中获得一年,月份和日期 JavaScript date. 根据您要查找的信息,您可以使用它们来了解两段时间之间的几个月。
例如, off-the-cuff, 它发现了多少
满月
在两个日期之间,不计算部分月 /例如,不包括每个日期的月份/:
/请注意,该月的值 JavaScript 开始S. 0 = 一月。/
在上述内容中包含小数月份更复杂,因为本月的典型2月份的三天都是如此 /~10.714%/, 八月三天 /~9.677%/,, 当然,即使是2月也是一个移动的目标,具体取决于他是否是闰年。
还有一些
http://www.datejs.com/
, 可以用来 JavaScript, 这可能有助于这项任务。
君笑尘
赞同来自:
请记住,本月的指数是基于 0. 代表着
和
.
石油百科
赞同来自:
1.If 您只需要两个日期之间的月数,不包括和 month1, 和 month2
2.If 你想包括任何月份
3.If 你想包括两个月
小明明
赞同来自:
例如,默认行为仅考虑了数月,例如, 3 几个月我。 1 一天会导致差异 3 月。 您可以通过设置参数来防止它
作为
, 所以差异 3 几个月我。 1 这一天将被返回 4 月。
上面接受的答案 /T.J. 奖金的回复/ 它不准确,有时它会返回错误的值。
例如,
回报
, 什么显然不正确。 正确的差异是 1 整整一个月或 2 舍入的月份。
这是我写的函数:
龙天
赞同来自:
这是一个扩展的答案版本。
, 但是在计算时纠正这种情况 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 月/
喜特乐
赞同来自:
总为几个月 start_date 和 end_date :
龙天
赞同来自:
窦买办
赞同来自:
它没有完全考虑到临时问题。 /例如, 3 Marta B. 4:00pm 和 3 四月 3:00pm/, 但它更准确,只有几行代码。
八刀丁二
赞同来自:
这将为您提供两个日期之间的几个月,忽略日期。
二哥
赞同来自:
如果您遍历日历,请增加一个月的开始日期 &, 看看我们是否经过最后日期。 这使得将异常的处理处理到嵌入式类别。 Date//, 但可能很慢
IF
你做了大量的日期。 詹姆斯的答案是基于这种方法。 我怎么喜欢这个想法,我认为这是一种方法 "safest", 如果你只做
一
计算,性能差异真的很微不足道。 我们倾向于尝试过度优化只能执行一次的任务。
现在,
如果一个
您在数据集中计算此功能,您可能不想在每行中运行此函数 /或者,上帝禁止,在记录中几次/. 在这种情况下,您可以在此处使用几乎任何其他答案。
除了
被接受的答案是错误的 /差差
和
平等的 -1/?
这是我对方法的打击 mathematical-and-quick, 这考虑了本月和闰年的不同长度。 如果您将其应用于数据集时,您真的必须使用类似的函数。 /让这个计算更多 & 时间/. 如果您只需要执行此操作,请使用上面的James迭代方法,因为您委派所有的处理 /许多/ 除了对象之外 Date//.
江南孤鹜
赞同来自:
你和另一种循环少的方法:
卫东
赞同来自:
在整数或数字术语之间返回几个月之间的差异
播送
开始的日期
作为第一个或最后一个
这是一个容错。 这意味着该函数仍将返回相同的值。
快网
赞同来自:
例如:
日期 1: 24/09/2015
/24 九月 2015/
日期 2: 09/11/2015
/9 十一月 2015/
区别: 2.5 /几个月/
冰洋
赞同来自:
君笑尘
赞同来自:
八刀丁二
赞同来自:
君笑尘
赞同来自:
}
八刀丁二
赞同来自:
奔跑吧少年
赞同来自:
卫东
赞同来自:
窦买办
赞同来自:
小明明
赞同来自:
在这种情况下,我对我不感兴趣的是整个月,不完整的几个月,月份持续多久,依此类推。我只需要知道月数。
现实世界中的相关案例是报告应该每月准备好,我需要知道应该有多少报告。
例子:
一月 = 1 月
一月 - 二月 = 2 月
十一月 - 一月 = 3 月
这是用于显示数字GOW的代码的详细示例。
让我们来 2 应该导致的时间标签 4 几个月
13 十一月 2019 当年 timestamp: 1573621200000
20 二月 2020 当年 timestamp: 1582261140000
也许有点不同于你的 timezone / 时间拉了。 日,几分钟和秒无关紧要,可以包括在内 timestamp, 但我们将忽略他们的实际计算。
步 1: 转换 timestamp 到 JavaScript 日期
步 2: 收到几个月的整数值 / 年
它给了我们
本月初: 11
开始年份: 2019
本月底: 2
结束年份: 2020
步 3: 添加
到底月。
这使我们开始的开始 11
它使我们上个月相等 15
步 4: 拉月亮
; 我们得到了4个月的结果。
29 一个月的例子例子
十一月 2019 年3月 2022 一年是 29 几个月。 如果你把它们放在桌子里 excel, 你会看见 29 学期。
我们的开始月11
我们上个月 - 40
40 - 11 = 29
石油百科
赞同来自:
詹大官人
赞同来自:
风见雨下
赞同来自:
http://joda-time.sourceforge.net/faq.html#datediff
计算两个日期之间的差异并从中致电此服务 javascript.
它表明你的背部是 Java.