Difference in Months Between Two Dates in JavaScript

Difference in Months between two dates in JavaScript

The definition of "the number of months in the difference" is subject to a lot of interpretation. :-)

You can get the year, month, and day of month from a JavaScript date object. Depending on what information you're looking for, you can use those to figure out how many months are between two points in time.

For instance, off-the-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

How to get full months between two dates in javascript

You can use something like this:

function monthDiff(d1, d2) {    var months;    months = (d2.getFullYear() - d1.getFullYear()) * 12;    months -= d1.getMonth() + 1;    months += d2.getMonth();    return months <= 0 ? 0 : months;}
var result = monthDiff( new Date(2008, 10, 4), new Date(2010, 2, 12) );
document.getElementById("result").innerHTML = result
<span id="result"></span>

How to calculate years and months between two dates in Javascript?

You will find a complete javascript function here with validation.

Edit: Link is dead - here is a simple JS line that calculates the difference in months between two dates:

return dateTo.getMonth() - dateFrom.getMonth() + 
(12 * (dateTo.getFullYear() - dateFrom.getFullYear()));

That is assuming that you have the dates in two variables called dateTo and dateFrom.

JavaScript: get all months between two dates?

This should produce the desired output:

function dateRange(startDate, endDate) {
var start = startDate.split('-');
var end = endDate.split('-');
var startYear = parseInt(start[0]);
var endYear = parseInt(end[0]);
var dates = [];

for(var i = startYear; i <= endYear; i++) {
var endMonth = i != endYear ? 11 : parseInt(end[1]) - 1;
var startMon = i === startYear ? parseInt(start[1])-1 : 0;
for(var j = startMon; j <= endMonth; j = j > 12 ? j % 12 || 11 : j+1) {
var month = j+1;
var displayMonth = month < 10 ? '0'+month : month;
dates.push([i, displayMonth, '01'].join('-'));
}
}
return dates;
}

Just call it with your existing date format:

dateRange('2013-11-01', '2014-06-01')
// ["2013-11-01", "2013-12-01", "2014-01-01", "2014-02-01", "2014-03-01", "2014-04-01", "2014-05-01", "2014-06-01", "2014-07-01", "2014-08-01", "2014-09-01", "2014-10-01", "2014-11-01", "2014-12-01"]

javascript get month between two dates

Please use these piece of code to get month difference

var date1=new Date(2017,1,19);//Remember, months are 0 based in JSvar date2=new Date(2017,4,19);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=(year2 - year1) * 12 + (month2 - month1) - 1;alert("Number of months "+numberOfMonths+1);


Related Topics



Leave a reply



Submit