1

I need to get data difference like years, month, date etc. like wise. this is my code

time1 =new Date(1988, 1, 8, 23, 23, 0, 0);
time2 = new Date();

difference = time2 - time1;
document.write(difference);

this returns something like this 782215145373. I think milliseconds. so how I get that difference like years, months, days, hours, minutes, seconds, milliseconds wise.

Actually in here I tried this way to get month difference

month_diff = time2.getMonth()-time1.getMonth();

but it gives 9 but correct answer should be 10. please help me why?

user1784592
  • 129
  • 1
  • 3
  • 11

1 Answers1

0

Try this :

 //your calculated difference in milliseconds
  var diff_ms = date2_ms - date1_ms;

  //take out milliseconds
  diff_ms = diff_ms/1000;

  var seconds = Math.floor(diff_ms % 60);

  diff_ms = difference_ms/60; 
  var minutes = Math.floor(diff_ms % 60);

  diff_ms = diffms/60; 

  var hours = Math.floor(diff_ms % 24);

  var days = Math.floor(diff_ms/24);
Abubakkar
  • 15,488
  • 8
  • 55
  • 83