887 B
887 B
title, tags
| title | tags |
|---|---|
| addDaysToDate | date,beginner |
Return the date of n days from any given date.
- Use
new Date()to read the first parameter as a valid Date - Add
nnumber of days to the given date - Return the output in
yyyy-MM-ddformat by usingDate.prototype.toISOString()and removing timestamp - For example, adding
15to1 October 2020results to16 October 2020 - Also accepts negative numbers. For example, adding
-15to16 October 2020results to1 October 2020
const addDaysToDate = (d,n) => {
d = new Date(d);
d.setDate(d.getDate() + n);
return d.toISOString().split('T')[0];
}
addDaysToDate("2020-10-15", -10); // '2020-10-05'
addDaysToDate("2020-10-15", 10); // '2020-10-25'
addDaysToDate("10/15/2020", 10); // '2020-10-25'
addDaysToDate("2020-10-31", 1); // '2020-11-01'
addDaysToDate("12/31/2020", 31); // '2021-01-31'