vijayjoshi.org - php | javascript | ajax | and all things web

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

Related Posts

Posted in : FAQ, Javascript, Technology
Tags: ,

10 Comments to “FAQ: calculate number of days between two dates in javascript”

Add Comments (+)

  1. SathiyaCG says:

    Thanks… Itz was very usefull

  2. Vijay says:

    @SathiyaCG hey thanks!!!

  3. Satish Puli says:

    Thanks it was very usefull
    Thanks again.

  4. jose says:

    thank you, but what if I am not from USA and the date is in this format (dd,mm,yyyy)?

  5. jose says:

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

  6. jose says:

    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

  7. NDK says:

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

  8. brusky says:

    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…

  9. Dipanjan Das says:

    what is the difference between the dates : 11JUN10 and 11JUN12 ?

Trackbacks/Pingbacks

  1. Tweets that mention calculate number of days between 2 dates in #javascript - -- Topsy.com

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

*

* Copy this password:

* Type or paste password here:

57,054 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>