JavaScript setDate()Method

Date 对象参考手册JavaScript Date Object

Example

Set the day of a month:

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

dOutput:


Try it Yourself »

Definition and Usage

The setDate() method sets the day of a month.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the setDate() method.


Syntax

Date.setDate(day)

Parameter Values

Parameter Description
day Required.

A numeric value representing the day of a month (1 - 31):

  • 0 is the last day of the previous month
  • -1 is the day before the last day of the previous month

If the current month has 31 days:

  • 32 is the first day of the next month

If the current month has 30 days:

  • 32 is the second day of the next month

Return Value

Type Description
Number The number of milliseconds between midnight January 1, 1970 and the adjusted date. Before ECMAScript standardization, this method did not return anything.

Technical Details

JavaScript Version: 1.0


More Examples

Example

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

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

dOutput:


Try it Yourself »

Example

In this example, we will set the day of the current month to 15 using the setDate() method:

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

dOutput:


Try it Yourself »


Date 对象参考手册JavaScript Date Object

Other Extensions