JavaScript: Get Number of Days Between Dates

Introduction

We oftentimes find ourselves counting the number of days from and to a date. Be it calculating when someone's due to return a book, when a subscription should be renewed, how many days have passed since a notification or when a new event is coming up.

In this tutorial, we'll take a look at how to get the number of days between dates in JavaScript.

The Date Object in JavaScript

A JavaScript Date is the Number of ticks (or milliseconds) that have elapsed since the beginning of the UNIX epoch (midnight on January 1, 1970, UTC).

Even though, at heart, a Date object is defined in terms of UTC, all of its methods fetch times and dates in the local time zone:

Date(); // Constructor for a new Date object
Date.now(); // Number of milliseconds elapsed since January 1, 1970 00:00:00 UTC

Now that we are familiar with the syntax, let's look at how to get the number of days between two dates using the Date object in JavaScript.

Number of Days Between Dates

To get the number of days between two dates, we'll make a simple function getNumberOfDays(), which accepts two Date objects:

function getNumberOfDays(start, end) {
    const date1 = new Date(start);
    const date2 = new Date(end);

    // One day in milliseconds
    const oneDay = 1000 * 60 * 60 * 24;

    // Calculating the time difference between two dates
    const diffInTime = date2.getTime() - date1.getTime();

    // Calculating the no. of days between two dates
    const diffInDays = Math.round(diffInTime / oneDay);

    return diffInDays;
}

console.log(getNumberOfDays("2/1/2021", "3/1/2021"));

This code results in:

28

The function accepts two Strings, which represent dates. We firstly create Date objects from these strings, after which, we calculate the number of milliseconds in a day. The getTime() function returns the time in milliseconds, between the start of the Unix epoch and current time. So, if we subtract the start date, from the end date, we'll get the number of milliseconds between them.

We can then turn this number of milliseconds, into days, by dividing it with the number of milliseconds in a day, resulting in the number of days between two dates.

Note: This approach includes the start date, but excludes the end date.

Get Number of Days Between Dates in JavaScript with js-joda

Developers familiar with Java will likely be familiar with the widely-used Joda-Time library, which was extremely popular before the Java 8 revamp of the Date/Time API.

Joda-Time's influence inspired the creation of js-joda - a general purpose date/time library for JavaScript, based on the ISO calendar system.

An added benefit is that it's extremely lightweight and really fast, and compared to other libraries such as Moment.js or date-utils, it provides its own implementation of date/time objects, instead of relying on the Date class from the native JavaScript implementation.

Let's import the library through vanilla JavaScript:

<script
    src="https://cdnjs.cloudflare.com/ajax/libs/js-joda/1.11.0/js-joda.min.js" 
</script>

Or, if you're using NPM:

Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

$ npm install js-joda

Now, we can use the js-joda API. Let's rewrite the previous function to use the LocalDate class, courtesy of js-joda:

const JSJoda = require('js-joda');
const LocalDate = JSJoda.LocalDate;

function getNumberOfDays(start, end) {
    const start_date = new LocalDate.parse(start);
    const end_date = new LocalDate.parse(end);

    return JSJoda.ChronoUnit.DAYS.between(start_date, end_date);
}

console.log(getNumberOfDays("2021-02-01", "2021-03-01"));

This also results in:

28

Note: This approach is also exclusive of the end_date.

Conclusion

In this tutorial, we've taken a look at how to get the number of days between dates in JavaScript. Other than the built-in approach, relying on the Date class, we've also explored the js-joda library, which was inspired by the Java-driven Joda-Time library, for a much more succinct approach to solving this problem.

Last Updated: October 4th, 2023
Was this article helpful?

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

Abhilash KakumanuAuthor

Hey, I am a full-stack web developer located in India. I am a curious person who is always trying to wrap my head around new technologies. In my free time, I read novels and play with my dog!

© 2013-2024 Stack Abuse. All rights reserved.

AboutDisclosurePrivacyTerms