FAQ: calculate number of days between two dates in javascript

From today onwards I am introducing a new category FAQ. It will contain quick solutions and code for common problems developers face daily in javascript and php. Browser compatibility issues specially (Firefox and IE) will also be taken care of.
So here is the first one.

Q) How can I get the difference(number of days) between two dates in javascript?

A) Date and time operations in javascript can be done through the Date object. Default constructor for Date doesn’t take any argument and returns current date.

var aDate = new Date(); // will give current date

To get a specific date, you can provide year, month and day.

var anotherDate = new Date(yyyy,mm,dd);

Take care of the fact that months are from 0-11 i.e. January is 0 and December 11.
Hence, new Date(2008,10,09) is 9th November 2008 not 9th October.
Since there is not any inbuilt function for calculating date difference in javascript, we will use getTime() function. getTime() returns number of milliseconds since midnight of January 1, 1970.
Below is the code

var oneDay = 24*60*60*1000;	// hours*minutes*seconds*milliseconds
var firstDate = new Date(2008,01,12);
var secondDate = new Date(2008,01,22);

var diffDays = Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay));

What we did is

  • we took two dates
  • got milliseconds for each using getTime() function
  • calculated their difference
  • and finally divided the difference with the number of milliseconds in one day

Simple, isn’t it???

14 thoughts on “FAQ: calculate number of days between two dates in javascript

  1. jose

    Ok ,so I split the date into 3 array elements, then i put it in a variable like this var date=new Date(arr[2], arr[1], arr[0]) and thats it.Great, thanx.I did the same thing like this: date=new Date(date.split(‘/’).reverse().join(‘,’));

    Reply
  2. jose

    I found out that the method: date=new Date(date.split(’/’).reverse().join(’,’)) only works in firefox not ie, because firefox accepts also strings in the date object, but ie doesn’t.So your method is the best method so far.
    var dates=firstDate.split(‘/’);
    var day = dates[0];
    var month = dates[1]-1;
    var year = dates[2];
    firstDate = new Date(year, month, day);
    This way the date object has inside paramaters not string

    Reply
  3. NDK

    If i try to do this it don’t work…

    For example:
    Current date is 2009,6,19
    Test date is 2009,6,21

    var date1=new Date(currentDate[2],(currentDate[1]-1),currentDate[0]);
    var date2=new Date(datetest[2],(datetest[1]-1),datetest[0])
    //I do -1 because: January is 0 and December 11

    Current date: 2009,6,19
    date: 2009,7,22
    date2 = -1325638800000
    date1 = -1420333200000
    Dif: 1096
    (= Math.abs((date1.getTime()-date2.getTime())/(one_day))

    Reply
  4. brusky

    hi friends,
    I need a function in javascript which must put live date into selectbox but user must select only 9 month higher not more so I will use it for pregnancy advise ,date must be max higher then user enter always will show 9 month
    thnks I need your helps…

    Reply
  5. Pingback: Tweets that mention calculate number of days between 2 dates in #javascript - -- Topsy.com

  6. Ponnaian R

    Hi,

    First of all i would like to say thanks for this.i tried this and got the out put in mozila but i am getting error in in IE.Please give me the solution my problem

    Thanks

    Reply