JavaScript Date setDate() method

Definition and usage

setDate() The method sets the day of the month to the date object.

Example

Example 1

Set the date in the month:

var d = new Date();
d.setDate(15);

Try it yourself

Example 3

Set the day of the current month to the last day of the previous month:

var d = new Date();
d.setDate(0);

Try it yourself

Example 3

Set the day of the month to a specified date:

var d = new Date("July 21, 1983 01:15:00");
d.setDate(15);

Try it yourself

Syntax

Date.setDate(day)

Parameter value

Parameter Description
day

Required. An integer representing the day of the month.

Expected value is 1-31, but other values are allowed:

  • 0 will result in the last day of the previous month
  • -1 will result in the day before the last day of the previous month

If a month has 31 days:

  • 32 will result in the first day of the following month

If a month has 30 days:

  • 32 will result in the second day of the following month

Technical details

Return value: A number representing the milliseconds between the date object and midnight on January 1, 1970.
JavaScript version: ECMAScript 1

browser support

method Chrome IE Firefox Safari Opera
setDate() Support Support Support Support Support

Related Pages

Tutorial:JavaScript Date

Tutorial:JavaScript Date Format

Tutorial:JavaScript Date Setting Methods