# Need to calculate the no of days between two dates?

• # April 19, 2009 at 2:52 pm

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

# April 19, 2009 at 5:30 pm

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

# April 19, 2009 at 5:41 pm

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)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".