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.