754 B
754 B
title, type, language, tags, cover, dateModified
| title | type | language | tags | cover | dateModified | |
|---|---|---|---|---|---|---|
| Add days to date | snippet | javascript |
|
digital-nomad-12 | 2020-11-28T19:18:29+02:00 |
Calculates the date of n days from the given date, returning its string representation.
- Use the
Dateconstructor to create aDateobject from the first argument. - Use
Date.prototype.getDate()andDate.prototype.setDate()to addndays to the given date. - Use
Date.prototype.toISOString()to return a string inyyyy-mm-ddformat.
const addDaysToDate = (date, n) => {
const d = new Date(date);
d.setDate(d.getDate() + n);
return d.toISOString().split('T')[0];
};
addDaysToDate('2020-10-15', 10); // '2020-10-25'
addDaysToDate('2020-10-15', -10); // '2020-10-05'