How To Add Days To A Date In TypeScript

How to add Days to a Date in TypeScript

When working with dates in TypeScript, there are situations where you have to add days into a given date. If you are struggling and don’t know how to add days to a date in TypeScript then don’t worry, because in this tutorial, we will give you some of the most common ways to do that.

How to add days to a date in TypeScript

Example 1: 

Let’s say we want to get the date of five days later from today.

First, we have to get the date of today, using the new Date() method.

// Get the date of today
var today = new Date();    

console.log(today);

Output:

Wed Oct 26 2022 01:12:01 GMT+0700 (Indochina Time)

The new Date() method will use the current date and time of the browser time zone to create a new date object.

Next, we will next add five days to the current date using these possible solutions:

Use the getDate() and setDate() method

After having the date of the current day, we can use the getDate() method to get the day-of-the-month of the current day.

The getDate() method will return the date-of-the-month of the given day. 

Syntax

Date.getDate()

Return value: the day-of-the-month of the given date.

// Get the date of today
var today = new Date();    

// Get the day-of-the-month of today
var todayDay = today.getDate();

console.log(todayDay);

Output:

26

If we want to add 5 days to this current day, simply add 5 to todayDay. Then we use the setDate() method to convert it into the date. 

The setDate method will set the date using the given day-of-the-month. 

Syntax

Date.setDate()

Return value: the date corresponding to the day-of-the-month.

// Get the date of today
var today = new Date();    

// Get the day-of-the-month of today
var todayDay = today.getDate();

var result = new Date();

// Set the date after adding 5 days
result.setDate(todayDay + 5);   

console.log(result);

Output:

Mon Oct 31 2022 13:18:27 GMT+0700 (Indochina Time)

JavaScript will automatically push the dates to next month and year. For example: 

// Get the date of today
var today = new Date();    

// Get the day-of-the-month of today
var todayDay = today.getDate();

var result = new Date();

// Set the date after adding 10 days
result.setDate(todayDay + 10);

console.log(result);

Output: 

Sat Nov 05 2022 13:18:47 GMT+0700 (Indochina Time)

Build a function

If we want to reuse our code multiple times, we can build a addDays function:

function addDays(oldDay, days)  {
  oldDay.setDate(oldDay.getDate() + 5);
  return oldDay;
}

// Get the date of today
var today = new Date();

console.log(addDays(today, 5))

Output: 

Mon Oct 31 2022 13:25:29 GMT+0700 (Indochina Time)

Summary 

In this tutorial, we have shown you how to add days to a date in TypeScript. The basic steps are: use the getDate() method to get the day-of-the-month of our date, add days, use the setDate() method to convert it into a date. 

Maybe you are interested:

Leave a Reply

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