##### treehouse : what would you like to learn today?
Web Design Web Development iOS Development

# Forums

Illustration by Nick Sirotich

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

• 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
• a little googling gave me this 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
• 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.

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