Javascript – Calculate the number of days between two dates

// Checkin date
var checkin = '2013-04-04';
var checkin_date = new Date();
checkin_date.setYear(parseInt(checkin.substr(0, 4), 10));
checkin_date.setMonth(parseInt(checkin.substr(5, 2), 10) - 1);
checkin_date.setDate(parseInt(checkin.substr(8, 2), 10));

// Checkout date
var checkout = '2013-04-10';
var checkout_date = new Date();
checkout_date.setYear(parseInt(checkout.substr(0, 4), 10));
checkout_date.setMonth(parseInt(checkout.substr(5, 2), 10) - 1);
checkout_date.setDate(parseInt(checkout.substr(8, 2), 10));

// Calculate the difference
var days = (checkout_date - checkin_date)/1000/60/60/24;

// Output the result
console.log(Math.floor(days));

 

Done =)

Reference: StackOverflow – jquery datepicker + date diff calculation

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s