0

I am trying to get months counts with two days like if i have two months with current date so i need a count of 2 like

var todaydate='20/06/2018';
var dynamicdate='05/08/2018';

i need months difference with this format can anyone help on this?

user3377261
  • 35
  • 1
  • 9
  • Also see: [moment js - two dates difference in number of days](https://stackoverflow.com/questions/36600687/moment-js-two-dates-difference-in-number-of-days/36600770) – mhodges Jun 19 '18 at 18:47
  • without momentjs plugin isn't possible? – user3377261 Jun 19 '18 at 18:47
  • Momentjs uses plain JS under the hood - it's just a JS library. So yes, technically it can be done, but it would be more work to code it yourself. How accurate are you looking to be? Do you want the difference in Months/Days, or Months as a decimal? Or Months as a rounded whole number, etc? – mhodges Jun 19 '18 at 18:49
  • If you just want to subtract the numbers of the months (i.e. `8-6 = 2`) you can just use `new Date("06/20/2018").getMonth() - new Date("08/05/2018").getMonth()` – mhodges Jun 19 '18 at 18:50
  • moment.js is good plugin, but using it to subtract two numbers is the hard way – Alex S. Jun 19 '18 at 18:56

1 Answers1

-1

Convert strings to Date object and use getMonth() method

var month1 = (new Date("2018-06-20")).getMonth();
var month2 = (new Date("2018-08-05")).getMonth();
var months = Math.abs(month2 - month1);

alert(months);

Or, even simpler:

var months = Math.abs(0 + todaydate.split('/')[1] - dynamicdate.split('/')[1]);

alert (months);
Alex S.
  • 622
  • 4
  • 6