English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript setDate() method

 JavaScript Date Object

ThesetDate()Method, sets the day of the month for the specified date (Date) object based on local time.

The expected value is1-31, but other values are allowed:

  • 0 represents the last day of the previous month

  • -1Represents the day before the last day of the previous month

If a month has31days:

  • 32Is the first day of the next month

If a month has30 days:

  • 32Is the second day of the next month

Syntax:

date.setDate(day)
var d = new Date();
d.setDate(22);
Test and see‹/›

Browser Compatibility

All browsers fully support the setDate() method:

Method
setDate()IsIsIsIsIs

Parameter Value

ParameterDescription
day1to31integer between the two dates, representing a day of the month.

If the value exceeds the date range of the month, the setDate() method will update the Date object accordingly.

For example, if the value is set to 0, the date will be set to the last day of the previous month.

Technical Details

Return Value:UTC 1970 Year1Month1Milliseconds between the date 00:00:00 and the given date
JavaScript Version:ECMAScript 1

More Examples

This example sets the day of the month to the last day of the previous month:

var d = new Date();
d.setDate(0);
Test and see‹/›

 JavaScript Date Object