Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Need to calculate the no of days between two dates?

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #24642
    victorymoon
    Member

    I had created a jquery calender by using

    http://jqueryui.com/demos/datepicker/

    I have some thing like this::

    Check in Date :: 21/04/2009
    Check out Date :: 25/04/2009

    I need to calculate the no of days between two dates?
    The Result should be something like this :: 4

    #56555
    chazzwick
    Member

    a little googling gave me this code:

    Code:
    var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds

    var firstDate = new Date(2009,04,21);
    var secondDate = new Date(2009,04,25);

    var diffDays = Math.abs((firstDate.getTime() – secondDate.getTime())/(oneDay)); //this calculates the number of days between firstDate and secondDate

    I tested it out, and it did give the answer as 4

    #56556
    chazzwick
    Member

    ive done a little playing around. If you make Jquery UI to output the date as YYYY/MM/DD, then you can use this code to modify it so the maths works.

    Code:
    var checkIn = “2009/04/21”;
    var checkInMod = new Date(checkIn.replace(///g, “,”));

    var checkOut = “2009/04/25”;
    var checkOutMod = new Date(checkOut.replace(///g, “,”));

    var oneDay = 24*60*60*1000;

    var diffDays = Math.abs((checkInMod.getTime() – checkOutMod.getTime())/(oneDay));

Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.