Files
30-seconds-of-code/snippets/addDaysToDate.md
Isabelle Viktoria Maciohsek c3a2e47672 Add prototype to descriptions
2020-10-20 11:21:07 +03:00

24 lines
646 B
Markdown

---
title: addDaysToDate
tags: date,intermediate
---
Calculates the date of `n` days from the given date, returning its string representation.
- Use `new Date()` to create a date object from the first parameter.
- Use `Date.prototype.getDate()` and `Date.prototype.setDate()` to add `n` days to the given date.
- Use `Date.prototype.toISOString()` to return a string in `yyyy-mm-dd` format.
```js
const addDaysToDate = (date, n) => {
d = new Date(date);
d.setDate(d.getDate() + n);
return d.toISOString().split('T')[0];
};
```
```js
addDaysToDate('2020-10-15', 10); // '2020-10-25'
addDaysToDate('2020-10-15', -10); // '2020-10-05'
```