How to get the difference between 2 Dates in Days using TypeScript

Get the difference between 2 Dates in Days using TypeScript

You are probably working with date data in your project. This tutorial will show you the method to get the difference between 2 Dates in Days using TypeScript. We will show you how to use the Number() function or getTime() method to do this. Keep reading for more information.

Get the difference between 2 Dates in Days using TypeScript

Date is a built-in object specialized for handling date and time in JavaScript. It is widely used because of its convenience and high applicability. In this tutorial, we give you the following two methods to get the difference between two Dates in Days.

Use the Number() function

The Number() function is used in TypeScript to convert data to numeric form.

Syntax:

Number(object)

When you pass a Date object as a parameter to the Number() method, it returns the number of milliseconds since 1.1.1970. Here is an example:

function getDiff(startDate: Date, endDate: Date): number {
    // Total number of milliseconds in a day
    const milSecInDay = 24 * 60 * 60 * 1000;

    // Convert the milliseconds to days
    const result = Math.abs(Number(endDate) - Number(startDate)) / milSecInDay;

    return Math.round(result);
}

const startDate = new Date("2022-11-22");
const endDate = new Date("2022-12-10");
console.log("The difference between the two Dates is: " + getDiff(endDate, startDate) + " days");

Output:

The difference between the two Dates is: 18 days

In the getDiff() function, we create a ‘milSecInDay’ variable to store the value of the total number of milliseconds in a day. 

The final result is the absolute value of the difference of the total number of milliseconds of each day when calling Number() and dividing by the ‘milSecInDay’ variable.

Use the getTime() method

Another approach for you is to use the getTime() method instead of the Number() function.

The getTime() method returns the number of milliseconds since January 1, 1970.

Syntax:

Date.getTime()

function getDiff(startDate: Date, endDate: Date): number {
    // Total number of milliseconds in a day
    const milSecInDay = 24 * 60 * 60 * 1000;

    // Convert the milliseconds to days
    const result = Math.abs(endDate.getTime() - startDate.getTime()) / milSecInDay;

    return Math.round(result);
}

const startDate = new Date("2022-11-22");
const endDate = new Date("2022-12-10");
console.log("The difference between the two Dates is: " + getDiff(endDate, startDate) + " days");

Output:

The difference between the two Dates is: 18 days

As you can see, the result you get is similar to the first method.

Summary

That’s the end of this tutorial. We just showed you how to use the Number() function and the getTime() method to get the difference between 2 Dates in Days using TypeScript. You can use either way depending on your preference. Hopefully, this tutorial will provide you with helpful information.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *