JavaScript & jQuery Tutorials

JS Home Right Arrow JS Reference Right Arrow Date Right Arrow setDate()

setDate()  Date  instance method

Description

Sets the locale specific day of the month (1-31) for the specified date.

Syntax


Signature Description
aDate.setDate(dayInt)Sets the locale specific day of the month (1-31) for the specified date.

Parameters


Parameter Description
dayIntAn integer in the range 1-31.
  • A value of 0 will set the date to the last day of the previous month.
  • Negative values will try and set the date to the last day of the previous month and then subtract integer amount.
  • For other values the setDate() method will try and set the date accordingly.

Examples

The code below shows examples of the setDate() method of Date.


// Create a Date instance for the current date and time.
var todaysDate = new Date();
alert(todaysDate);

// Set Day of the month.
todaysDate.setDate(-6);
alert(todaysDate + ' - Day set to -6');

todaysDate.setDate(0);
alert(todaysDate + ' - Day set to 0');

todaysDate.setDate(35);
alert(todaysDate + ' - Day set to 35');

Press the button below to action the above code:


Related Tutorials


JavaScript Intermediate Tutorials - Lesson 2 - Dates and Times



  

go to home page Homepage go to top of page Top