• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Justin Joyce

Practical tips and tutorials about software development.

  • Standing Invitation
  • Featured Posts
  • Latest
  • About

Calculate Date Difference in Javascript

Posted Jan 19, 2023 — Updated Jan 10, 2024

You have two main options:

  1. Use an npm package1 like luxon, day.js, or date-fns
  2. Convert each date to milliseconds, subtract, and multiply

Let’s go through a quick example of each approach.

Using date-fns to get date difference

Above, I mentioned luxon, day.js, and date-fns. They’re all great packages, but I personally prefer date-fns due to its easily searchable documentation.

const {
    differenceInDays,
    differenceInHours,
    differenceInMinutes,
} = "date-fns";

const twentyThree = new Date("2023/01/01");
const twentyTwo = new Date("2022/01/01");

differenceInDays(twentyThree, twentyTwo);
// 365

differenceInHours(twentyThree, twentyTwo);
// 8760

differenceInMinutes(twentyThree, twentyTwo);
// 525600

date-fns has a ton of other functionality and the docs are very good (and searchable!), I’d recommend using it.

Using pure javascript

This is a bit more convoluted, but doesn’t require any additional packages. It does, however, require a bit of knowledge of what Unix Time is. To plagiarize wikipedia:

Unix Time […] measures time by the number of seconds that have elapsed since 00:00:00 UTC on 1 January 1970, the beginning of the Unix epoch, less adjustments made due to leap seconds.

Now that you know that, let’s get into it:

const twentyThree = new Date("2023/01/01");
const twentyTwo = new Date("2022/01/01");

// Convert both dates to millisecond Unix time
// I know I said "seconds" above, but JS does it in MS ¯\_(ツ)_/¯
const twentyThreeMs = twentyThree.getTime();
const twentyTwoMs = twentyTwo.getTime();

// calculate the difference
const diffMs = twentyThreeMs - twentyTwoMs;

const msPerDay = 1000 * 60 * 60 * 24;
// 1000 ms per second
// 60 seconds per minute
// 60 minutes per hour
// 24 hours per day

const differenceInDays = diffMs / msPerDay;
// 365

const msPerHour = 1000 * 60 * 60;
const differenceInHours = diffMs / msPerHour;
// 8760

// Minutes
console.log(diffMs / 60000);
// 525600

Not too bad, even in plain JS. In larger projects though, there’s a good chance you’ll be doing other operations on dates, and a nice npm package like date-fns will definitely make your life easier.


Notes

  1. For years, moment.js was the be-all and end-all of javascript date manipulation. However, it’s now in maintenance mode, and moment’s maintainers themselves recommend the three alternatives posted above. ↩︎

Helpful Links

What is Epoch (Unix) Time? – wikipedia

Date-fns documentation

Filed Under: Dates, Javascript

Primary Sidebar

Recent Posts

  • Every Built-In Vim Color Scheme (with screenshots)
  • Reverse a string in Python
  • Meeting Cost Calculator
  • Vim find and replace
  • What makes an effective development team

Categories

  • Arrays (5)
  • Command Line (9)
  • Dates (3)
  • Featured (7)
  • Git (7)
  • Golang (5)
  • Javascript (8)
  • Productivity (8)
  • Projects (4)
  • Python (15)
  • Regex (2)
  • Ruby (3)
  • Shell (2)
  • Thoughts (2)
  • Tips (11)
  • Tools (3)
  • Tutorials (1)
  • Vim (4)

Archives

  • July 2024 (1)
  • February 2024 (1)
  • January 2024 (1)
  • December 2023 (1)
  • November 2023 (1)
  • October 2023 (4)
  • September 2023 (1)
  • August 2023 (2)
  • July 2023 (5)
  • June 2023 (3)
  • May 2023 (6)
  • April 2023 (5)
  • March 2023 (5)
  • February 2023 (10)
  • January 2023 (6)
  • December 2022 (7)

Copyright © 2025 · Contact me at justin [at] {this domain}

  • Privacy Policy