Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript setDate() method

JavaScript setDate() method

Date Object Reference JavaScript Date object

Example

Set the day of the month:

var d = new Date();

d.setDate(15);

d Output result:

Try it »


Definition and Usage

The setDate() method is used to set the day of the month.


Browser support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the setDate() method


Syntax

Date

.setDate(

day

)

Parameter value

Parameters Description
day Required.

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

  • 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:

    If the current month has 30 days:

    |

    Return Value

    Type Description
    Number The number of milliseconds from midnight January 1, 1970 to the adjusted date. Before ECMAScript standardization, this method returned nothing.

    Technical Details

    JavaScript version: 1.0

    More examples

    Example

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

    var d = new Date();

    d.setDate(0);

    d Output result:

    Try it »

    Example

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

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

    d.setDate(15);

    d Output result:

    Try it »


    Date Object Reference JavaScript Date object