2
var frommonth="201912";
var tomonth="201810";

From Above Two Month how i will get difference between two Month in JavaScript?

var date1 = new Date(fromdate);
var date2 = new Date(todate);
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 

var fromYear=date1.getFullYear();
var toYear=date2.getFullYear();
var diffyear =toYear-fromYear;
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
Tarik
  • 139
  • 1
  • 12
  • 1
    Please don't tag javascript questions with java. Even though there is 'java' in the name 'javascript', the two languages are not the same. they are not even related. – Stultuske Dec 19 '19 at 07:32
  • The two dates probably won't be what you need. What date are you trying to specify? Also, do you use moment.js? (Yes you tagged it but don't use it in your code...) – sandrooco Dec 19 '19 at 07:36

2 Answers2

2

new Date() dont parse YYYYMM. It consider 201912 as year

So Use match() to parse YYYYMM

var from = "201912";
var to = "201810";

function parseMonth(str) {
  return str.match(/(\d{4})(\d{2})/).splice(1).map(Number)
}

var [fromY, fromM] = parseMonth(from)
var [toY, toM] = parseMonth(to)

var result = (fromY - toY) * 12 + (fromM - toM)

console.log(result, 'months')
User863
  • 19,346
  • 2
  • 17
  • 41
0

You can pass the frommonth and tomonth to the function as parameters and can perform calculation of difference..

Here new Date(frommonth.substring(0,4), frommonth.substring(4), 0) denotes,

-> frommonth.substring(0,4) => Getting the year from the string

-> frommonth.substring(4) => Getting the month from the string

-> 0 => Setting up date as 0.

And the same has been considered for tomonth as well..

Also Math.round(timeDiff / (2e3 * 3600 * 365.25)); is made to consider the leap year as well..

const frommonth = "201912"; 
const tomonth = "201810";


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

const result = diffInMonths(new Date(frommonth.substring(0,4), frommonth.substring(4), 0), new Date(tomonth.substring(0,4), tomonth.substring(4), 0));

//Diff in months
console.log(result);
Maniraj Murugan
  • 8,868
  • 20
  • 67
  • 116
  • 1
    365.25 doesn't account for leap years. There are no years that have 1/4 of a day in them. Also 365.25 is the length of an average *Julian* year. In the modern Gregorian calendar, the average length is 365.2425 days. But still, no *average* year is what will happen in any given year. (Rounding only helps when the result happens to align in your favor. Over a large enough difference, the rounding will produce the wrong result.) – Matt Johnson-Pint Dec 20 '19 at 00:33
  • @MattJohnson-Pint, I have made it as a reference from this answer https://stackoverflow.com/a/54706607/7785337 and made this change for leap year support. But as I am not well versed about the date, timediff , I may be slightly wrong with it.. Happy to learn from you about it.. Thanks !!.. – Maniraj Murugan Dec 20 '19 at 01:05