0

I have tried all the solutions in the stack overflow, but couldn't find the exact solution or answer for my question.Some times it shows negative values when i tried to convert the date using getTime(), and sometimes the number of days showed in the answers seems to be incorrect compared to the manually calculated one. I've added the code that've used for my calculation. Please make a look.

Referred : Difference between two dates in years, months, days in JavaScript

var date2 = new Date("2018-01-01");
var date1 = new Date("2017-01-01");


var diff = Math.floor(date2.getTime() - date1.getTime());
//1000*60*60*24
var secs = Math.floor(diff / 1000);
var mins = Math.floor(secs / 60);
var hours = Math.floor(mins / 60);
var days = Math.floor(hours / 24);
var months = Math.floor(days / 31);
var years = Math.floor(months / 12);
months = Math.floor(months % 12);
days = Math.floor(days % 31);
hours = Math.floor(hours % 24);
mins = Math.floor(mins % 60);
secs = Math.floor(secs % 60);
var message = "";
if (days <= 0) {
  message += secs + " sec ";
  message += mins + " min ";
  message += hours + " hours ";
} else {
  message += days + " days ";
  if (months > 0 || years > 0) {
    message += months + " months ";
  }
  if (years > 0) {
    message += years + " years ago";
  }
}
console.log(message);

//Expected result is : 1 year 0 months 0 days.


//Actual result is : 11 months 24 days.
georgeawg
  • 48,608
  • 13
  • 72
  • 95
BatmanForLife
  • 99
  • 2
  • 3
  • 12
  • 1
    wouldn't the expected result be 1 year 0 months 0 days? ... `var months = Math.floor(days / 31);` .. but only 7 months have 31 days, so that's an odd calculation – Jaromanda X May 08 '19 at 05:02
  • @Jaromanda X yeah it's corrected now! how to differentiate 31 days , 30days, 28days? – BatmanForLife May 08 '19 at 05:07

2 Answers2

-1

There aren't 31 days in a month. In a non-leap year there are 30.417 days.

-2

Try like this, but unlike your expected result.12 months which is equal to 1 year.

the result of my code is 1 Year 0 Months 0 Days.

var date2 = new Date("2021-01-01")
var date1 = new Date("2020-01-01");

function getdiff(date2, date1) {

  var yeardate2 = date2.getYear();
  var monthdate2 = date2.getMonth();
  var datedate2 = date2.getDate();
  var yeardate1 = date1.getYear();
  var monthdate1 = date1.getMonth();
  var datedate1 = date1.getDate();
  var diff = {};
  var res = "";
  var yearString = "";
  var monthString = "";
  var dayString = "";


  yearTot = yeardate2 - yeardate1;

  if (monthdate2 >= monthdate1)
    var monthTot = monthdate2 - monthdate1;
  else {
    yearTot--;
    var monthTot = 12 + monthdate2 -monthdate1;
  }

  if (datedate2 >= datedate1)
    var dateTot = datedate2 - datedate1;
  else {
    monthTot--;
    var dateTot = 31 + datedate2 - datedate1;

    if (monthTot < 0) {
      monthTot = 11;
      yearTot--;
    }
  }

  diff = {
    years: yearTot,
    months: monthTot,
    days: dateTot
  };

  if ( diff.years > 1 ) yearString = " years ";
  else yearString = " year ";

  if ( diff.months> 1 ) monthString = " months ";
  else monthString = " month ";

  if ( diff.days > 1 ) dayString = " days ";
  else dayString = " day ";
   
  
  res = diff.years + yearString  + diff.months + monthString + diff.days + dayString;

  return res;
}


console.log(getdiff(date2, date1))
Syed mohamed aladeen
  • 6,507
  • 4
  • 32
  • 59