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

`getTime()`

methodAnother 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**:

- Calculate the time between 2 Dates in TypeScript
- Get the current date and time in TypeScript
- How to add Days to a Date in TypeScript

My name’s Christopher Gonzalez. I graduated from HUST two years ago, and my major is IT. So I’m here to assist you in learning programming languages. If you have any questions about Python, JavaScript, TypeScript, Node.js, React.js, let’s contact me. I will back you up.

**Name of the university: **HUST

**Major**: IT

**Programming Languages**: Python, JavaScript, TypeScript, Node.js, React.js