1

How can I get the difference between two dates?

I have:

date1 = 2014/2/20
date2 = 2014/2/16

I want to get the difference value as 4.

Felix
  • 37,892
  • 8
  • 43
  • 55

3 Answers3

3

Try this to get the difference in terms of number of days

var date1= "2014/2/20";
var date2= "2014/2/16";

var difference = (new Date(date1) - new Date(date2))/(1000*60*60*24);

Js Fiddle Demo

Sachin
  • 40,216
  • 7
  • 90
  • 102
1

Fiddle Demo

var oneDay = 24 * 60 * 60 * 1000;    
function getDateDiffinDays(d1, d2) {
    var firstDate = new Date(d1);
    var secondDate = new Date(d2);
    return Math.round(Math.abs((firstDate.getTime() - secondDate.getTime()) / (oneDay)));
}

alert(getDateDiffinDays('2014/2/20', '2014/2/16'));
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
0

You may simply get the day month and year from the date using js methods as getMonth,year etc. after this you can sinply subtract.

Zeshan Khan
  • 294
  • 2
  • 15