JavaScript

How to count the number of days between two dates in JavaScript code?

There are times when you are coding an app, you are required to count the number of days between two specific dates. For example, you are creating an application for a library and in that app, you have to calculate the number of days when someone returns a book or when a member’s subscription is required to be renewed, or when a new event comes up.

In JavaScript, to calculate the number of days between two dates we can use the date object hence let us start our article by defining what a date object is in JavaScript.

What is a Date Object

Date object is a built-in JavaScript method that allows us to work with dates and times and is initialized with the keyword new. An example of initializing a date object is given below:

const myDate = new Date(); // declaring date

The new Date() will create an object that will have the current date and time:

const myDate = new Date(); // declaring date
console.log(myDate);

It’s also worth noting that the Date object just represents the amount of milliseconds from January 1, 1970.

Count Number of days between two dates

Now that we know what date the object is, let us go through an example where we will count the number of days between two Dates. For this purpose, we will first define a function with the name of getDays() and pass two arguments to this function i-e a starting date and an ending date as strings. Then in this function, we will perform other operations to calculate the number of days between the starting and ending days:

function getDays(start, last) {
    //initialize dates with Date object
    const date1 = new Date(start);
    const date2 = new Date(last);

    // calculation for converting a day into milliseconds
    const oneDay = 1000 * 60 * 60 * 24;

    // calculation for the time difference between start and last
    const diffTime = date2.getTime() - date1.getTime();

    // calculation for the days between start and last
    const diffDays = Math.round(diffTime / oneDay);
    // return number of days
    return diffDays;
}
const myDate= new Date(); // current time that is 10/26/2021
console.log(getDays("2/1/2021", "3/1/2021")); // 28
console.log(getDays("10/24/2021", myDate)); // 3

In the above code, inside the getDays() function we are first declaring two dates using the arguments passed in the getDays() function. After that, we are calculating milliseconds for one day and then the difference in time between the two days. Finally, we are calculating the number of days between the two dates, storing the value in diffDays, and in the end, we return the diffDays variable. Outside the function, we initialized a Date object which will store the current date and time and then we called the getDays() function inside the console log by giving the getDays() two date arguments as strings. We also call the getDays() function with a random date and myDate arguments. The result is as follow:

Conclusion

Counting the number of days between two dates in JavaScript is a good practice program for a new JavaScript developer. Creating a program that calculates the number of days between two dates is very helpful in applications where you have to calculate the number of days and also it helps in getting acquainted with the Date object of javascript.

In this post, we first taught you what a date object is in JavaScript, and then we went on and told you how to count the number of days between two dates in JavaScript using the Date object.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.