Calculate difference between two dates using Javascript

Difference between two dates

Input String: calculateDays(“01-01-2014″,”12-12-2014”)

Date Format: MM-DD-YYYY

function calculateDays(datetime1, datetime2) {
var oneDay = 1000 * 60 * 60 * 24;        // The number of milliseconds in a day
//Convert the datetime1 and datetime2 to Date object and get Time in milliseconds

var dt1 = new Date(datetime1).getTime();
var dt2 = new Date(datetime2).getTime();

// Calculate the difference in milliseconds
var diff = Math.abs(dt1- dt2); // Difference of Days

return Math.round(diff / oneDay);
}

Advertisement

9 thoughts on “Calculate difference between two dates using Javascript

  1. Top notch blog post.Thanx just for penning this useful article in addition to enlightening us all with the feelings.I’m hoping you are going to proceed this great work at a later date as well.

    Like

  2. “Great Blogpost! Hi there! This is my first comment here so I just wanted to give a quick shout out_ and tell you I genuinely enjoy reading through your posts. Can you recommend any other blogs/websites/forums that deal with the same topics?”

    Like

  3. “Great Blogpost! Hi there! This is my first comment here so I just wanted to give a quick shout out_ and tell you I genuinely enjoy reading through your posts. Can you recommend any other blogs/websites/forums that deal with the same topics?”

    Like

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 )

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.