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


Discover more from Arun Potti's Power Platform blog

Subscribe to get the latest posts to your email.

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.

  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?”

  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?”

Leave a Reply