Javascript - Date setDate() Method

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

Description

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

Syntax

Date.setDate(day)

Parameter Values

Parameter Require Description
day Required. An integer representing the day of a month.

Expected values are 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 the month has 31 days: 32 will result in the first day of the next month
  • If the month has 30 days: 32 will result in the second day of the next month

Return

A Number, representing the number of milliseconds between the date object and midnight January 1 1970

Example

Set the day of the month:

Demo

//display the date after changing the day of the month.
var d = new Date();
d.setDate(15);/*from w ww . ja  v a2s . co  m*/
console.log(d);

//Set the day of the month to be the last day of the previous month:
d.setDate(0);
console.log(d);

//Set the day of the month, to a specified date:
var d = new Date("July 21, 2010 01:15:00");
d.setDate(15);
console.log(d);

Result