0

typescript / javascript

I Received a date like this "1500074035"

I Want to be able to know , how much months have been passed from todays , since this date.

function diff_months(dt2, dt1) 
 {

  var diff =(dt2.getTime() - dt1.getTime()) / 1000;
   diff /= (60 * 60 * 24 * 7 * 4);
  return Math.abs(Math.round(diff));

 }

dt1 = new Date(2014,10,2);
dt2 = new Date(2014,10,11);
console.log(diff_months(dt1, dt2));

dt1 = new Date("June 13, 2014 08:11:00");
dt2 = new Date("October 19, 2014 11:13:00");
console.log(diff_months(dt1, dt2));


but this is wrong .. when the years are different .

i think my problem is here 

   diff /= (60 * 60 * 24 * 7 * 4);
  • 2
    what do you already try ? show us some code – Naor Tedgi May 13 '18 at 10:15
  • Possible duplicate of [Calculate age given the birth date in the format YYYYMMDD](https://stackoverflow.com/questions/4060004/calculate-age-given-the-birth-date-in-the-format-yyyymmdd) – Tigger May 13 '18 at 10:23
  • function diff_months(dt2, dt1) { var diff =(dt2.getTime() - dt1.getTime()) / 1000; diff /= (60 * 60 * 24 * 7 * 4); return Math.abs(Math.round(diff)); } dt1 = new Date(2014,10,2); dt2 = new Date(2014,10,11); console.log(diff_months(dt1, dt2)); dt1 = new Date("June 13, 2014 08:11:00"); dt2 = new Date("October 19, 2014 11:13:00"); console.log(diff_months(dt1, dt2)); but this is wrong .. when the years are different . i think my problem is here diff /= (60 * 60 * 24 * 7 * 4); – Anan Kassis May 13 '18 at 11:06

0 Answers0