0

I would like to have the number of months between two dates. I have seen many post on stackoverflow but no one is my case. I would like to have this solution:

From 2017-01-19 to 2017-01-23 = 1 Month
From 2017-01-19 to 2017-02-15 = 2 Months
From 2017-01-30 to 2017-02-28 = 2 Months
From 2017-01-19 to 2019-03-01 = 27 Months

I hope that I was clear

Francesco G.
  • 679
  • 3
  • 13
  • 32

3 Answers3

2

Your test cases are somewhat unclear. BTW if you want to just get the difference between months then you can achieve it in following way. I'm assuming that you have your dates as var d1 = "2017-01-19", d2 = "2017-01-23"; Then you can do the following:

var date1 = d1.split("-");
var date2 = d2.split("-");
// assuming that you don't know which date comes first
// otherwise no need to use Math.abs()
var months = Math.abs(date2[0] - date1[0]) * 12 + Math.abs(date2[1] - date1[1]) + 1;

or this can be done:

//assuming a month of 30 days
var months = Math.floor(Math.abs(new Date(d1) - new Date(d2)))/(1000 * 60 * 60 *24 * 30) + 1;
Ravi Kumar Seth
  • 154
  • 2
  • 14
  • why abs it can jst be month = new Date(d1) - new Date(d2) – Abhinav Mar 22 '17 at 17:44
  • 2
    you can do it this way too month = (new Date(d1) - new Date(d2))/(1000 * 60 * 60 *24) + 1; (assuming that you know which date comes first... Math.abs() is for cases in which you don't know which date comes first) as (new Date(d1) - new Date(d2)) will be in "ms" (can be positive, negative or zero depending on which date comes first or if both dates are same) – Ravi Kumar Seth Mar 22 '17 at 18:37
  • interesting.. this is somewhat I was not knowing.. thanks for this learning.. – Abhinav Mar 22 '17 at 18:44
2

Please use these piece of code to get month difference

var date1=new Date(2017,1,19);//Remember, months are 0 based in JS
var 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);
Dilip D
  • 567
  • 1
  • 5
  • 28
  • how could you relate this to OP question? – Abhinav Mar 22 '17 at 17:41
  • 1
    thanks for support, I have edited your code like this: if(numberOfMonths<=0){ return 1; }else{ return (numberOfMonths+1); } – Francesco G. Mar 22 '17 at 17:43
  • What is the point of creating Date objects only to get back the year and month as passed in? There is no need to add 1 to the month, it's the difference that matters, not the absolute value. – RobG Mar 22 '17 at 20:29
0

Assuming 1 month = 30 days you can do

var dates = [
  {from: '2017-01-19', to: '2017-02-23'},
  {from: '2017-01-19', to: '2017-02-15'},
  {from: '2017-01-30', to: '2017-02-28'},
  {from: '2017-01-19',to: '2019-03-01'}
];

dates.forEach(function(item) {
  var from = new Date(item.from);
  var to = new Date(item.to);
  var diffMonth = Math.round((to - from) / (1000 * 60 * 60 * 24 * 30));

  console.log(
    from.toJSON().split('T')[0],
    'to',
    to.toJSON().split('T')[0],
    diffMonth + ' month' + (diffMonth > 1 ? 's' : '')
  );
});
Ionut
  • 1,729
  • 4
  • 23
  • 50