Categories
TypeScript Answers

How to add a day to a date in TypeScript?

Spread the love

Sometimes, we want to add a day to a date in TypeScript.

In this article, we’ll look at how to add a day to a date in TypeScript.

How to add a day to a date in TypeScript?

To add a day to a date in TypeScript, we can call the getDate and setDate methods.

For instance, we write

const addDays = (date: Date, days: number): Date => {
  date.setDate(date.getDate() + days);
  return date;
};

to define the addDays function that takes the number of days to add to the date.

We get the date of date with getDate.

Then we add days to the date returned by getDate and use the sum as the argument of setDate to add the days to date.

And then we return date.

Conclusion

To add a day to a date in TypeScript, we can call the getDate and setDate methods.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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