Add days to date

JavaScript, Date · Nov 28, 2020

Calculates the date of n days from the given date, returning its string representation.

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'

More like this

  • Add minutes to date

    Calculates the date of n minutes from the given date, returning its string representation.

    JavaScript, Date · Nov 28, 2020

  • Add weekdays to date

    Calculates the date after adding the given number of business days.

    JavaScript, Date · Jan 7, 2021

  • Days ago

    Calculates the date of n days ago from today as a string representation.

    JavaScript, Date · Jan 30, 2022