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.

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

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

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???

Related Posts

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

    1. Hi,

      Is there a way to calculate the difference between two dates selected by the user and then output the result into a third field? So the user picks the starting date, then he picks the ending date, and automatically the result will be generated in another filed.

      Thank you

  1. 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(‘,’));

  2. 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

  3. 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))

  4. 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…

  5. 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


Leave a Reply