Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Date.prototype.setDate

Sets the day of the month — handy for adding and subtracting days.

Available since: ES1

Syntax

date.setDate(day)

Parameters

ParameterTypeDescription
daynumberThe day of the month. 0 is the last day of the previous month, 32 rolls into the next one.

Return value

numberThe new timestamp. The object itself changes.

Examples

In 20 days — rolling into the next month:

const date = new Date(2026, 2, 15);
date.setDate(date.getDate() + 20);
 
console.log(date.getMonth() + 1, date.getDate());

Output:

4 4

A week ago:

const date = new Date(2026, 2, 3);
date.setDate(date.getDate() - 7);
 
console.log(date.getMonth() + 1, date.getDate());

Output:

2 24

Notes

Overflow is handled automatically: the month and year are recalculated. It is the most reliable way to add days, including across daylight saving changes.

See also

Related recipes

In the tutorial

  • Dates and time — The Date object in JavaScript: creating a date, reading the year, month and day, changing a date, the difference between dates and the ISO format.

Thanks for reading!

Donate to the site — we work hard for you.