Skip to Content

JavaScript Date Difference Calculations

In this tutorial, you'll learn how to calculate the difference between two dates using vanilla JavaScript. We'll cover difference calculations for seconds, minutes, hours, days, weeks, months, and years, and wrap it up with a function that returns them all in a single object. Let's get started!

Date Definitions

First, let's start with a couple of date definitions. We'll create two dates, assigning the first date to the date1 variable, and the second date to the date2 variable:

var date1 = new Date("2022-03-12 12:30:00");
var date2 = new Date("2022-04-11 11:05:30");

Now let's walk through the different date calculations you can perform.

Difference in Seconds

Here's how to calculate the difference between two dates in seconds:

var one_second = 1000;
var diff = Math.round((date2 - date1) / one_second);

console.log(diff);
// 2583330

Difference in Minutes

Here's how to calculate the difference between two dates in minutes:

var one_minute = 1000 * 60;
var diff = Math.round((date2 - date1) / one_minute);

console.log(diff);
// 43056

Difference in Hours

Here's how to calculate the difference between two dates in hours:

var one_hour = 1000 * 60 * 60;
var diff = Math.round((date2 - date1) / one_hour);

console.log(diff);
// 718

Difference in Days

Here's how to calculate the difference between two dates in days:

var one_day = 1000 * 60 * 60 * 24;
var diff = Math.round((date2 - date1) / one_day);

console.log(diff);
// 30

Difference in Weeks

Here's how to calculate the difference between two dates in weeks:

var one_week = 1000 * 60 * 60 * 24 * 7;
var diff = Math.round((date2 - date1) / one_week);

console.log(diff);
// 4

Difference in Months

Here's how to calculate the difference between two dates in months:

var diff = ((date2.getFullYear() - date1.getFullYear()) * 12) + (date2.getMonth() - date1.getMonth());

console.log(diff);
// 1

Here, instead of running a calculation, we're utilizing JavaScript's getFullYear() method to subtract the first date from the second date, then adding the remaining months with the getMonth() function.

Difference in Years

Here's how to calculate the difference between two dates in years:

var diff = date2.getFullYear() - date1.getFullYear();

console.log(diff);
// 0

Again, we're utilizing JavaScript's getFullYear() method to subtract the first date from the second date, giving us the difference in years.

Our Custom Date Calculation Function

Putting it all together, we're going to create a custom function that runs all of these calculations and methods at once, then returns the data in a single object:

var date1 = new Date("2022-03-12 12:30:00");
var date2 = new Date("2022-04-11 11:05:30");

function calcDates(date1, date2) {
date1 = new Date(date1);
date2 = new Date(date2);

var one_second = 1000;
var one_minute = 1000 * 60;
var one_hour = 1000 * 60 * 60;
var one_day = 1000 * 60 * 60 * 24;
var one_week = 1000 * 60 * 60 * 24 * 7;

var result = {
seconds: Math.round((date2 - date1) / one_second),
minutes: Math.round((date2 - date1) / one_minute),
hours: Math.round((date2 - date1) / one_hour),
days: Math.round((date2 - date1) / one_day),
weeks: Math.round((date2 - date1) / one_week),
months: ((date2.getFullYear() - date1.getFullYear()) * 12) + (date2.getMonth() - date1.getMonth()),
years: date2.getFullYear() - date1.getFullYear()
};

return result;
}

var diff = calcDates("2022-03-12 12:30:00", "2022-04-11 11:05:30");

console.log(diff);

/* output:
{
days: 30,
hours: 718,
minutes: 43056,
months: 1,
seconds: 2583330,
weeks: 4,
years: 0
}
*/

Conclusion

That's all there is to calculating the difference between two dates in JavaScript! You can find this function uploaded to this GitHub repository.

Created: April 11, 2022

Comments

There are no comments yet. Start the conversation!

Leave a Comment

Comment Etiquette: Wrap code in a <code> and </code>. Please keep comments on-topic, do not post spam, keep the conversation constructive, and be nice to each other.