Hi Friends 👋,

Welcome To Infinitbility! ❤️

To get days between two dates,

  1. Get both dates as a time using the getTime() method.
  2. subtract them which you get using the getTime() method.
  3. Devied your output time with / 1000 * 3600 * 24

It will return your difference in dates in a number of days.

The getTime() method returns the number of milliseconds since the ECMAScript epoch.

You can use this method to help assign a date and time to another Date object. This method is functionally equivalent to the valueOf() method.

Today, I’m going to show How do I get days between two dates in javascript, here I will use the javascript getTime() method and the above-mentioned conditions to get days between the start and end date time.

Let’s start the today’s tutorial How do you get days between two dates in javascript?

In the following example, we are going to do

  1. create two dates starting and ending date
  2. create a function that accepts a date and returns days
  3. Use a function for multiple date examples

let’s write the code.

function getDays(startingDate, endingDate) {
    return parseInt(Math.abs(endingDate.getTime() - startingDate.getTime()) / (1000 * 3600 * 24));

const startDate = new Date("2021-08-25 07:41:07");
const endDate =  new Date("2021-08-30 09:50:07");

getDays(startDate, endDate); // ✅ 5

In the above program, we have created a custom function getDays() and pass dates to get a mumber of days.

let’s check the output.

javascript, get days between two dates example
javascript, get days between two dates example

I hope it’s help you, All the best 👍.