Snippets
Created by
Melodi Pace
- Title: addDaysToDate
- Tags: date,intermediate
- online resize image
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 argument. - Use
Date.prototype.getDate()
andDate.prototype.setDate()
to addn
days to the given date. - Use
Date.prototype.toISOString()
to return a string inyyyy-mm-dd
format.
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'
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.