Now you can listen to this article with the Online Text Reader

JavaScript setDate Method

The setDate method sets the day of the month of the point in time stored in a Date object.

Besides, it can also be used to increase or decrease the point in time of that object in a given quantity of days.

The value returned by this method is the quantity of milliseconds passed since January 1st 1970 until the point in time stored in the Date object once it has been modified.


myDate.setDate( days )

  • days being an integer number that, when it is between 1 and the last day of the month in question (which can be 28, 29, 30, or 31), changes only the day of the month of the point in time stored in a Date object. Any value outside that range increases or decreases the point in time of a Date object in a quantity equal to the range's limit being exceeded plus the remaining days.
  • myDate being an object based on the Date object.


The following code uses the setDate method to change the day of the month of the current date. If the current day of the month is less than 15, it changes it to 28. On the other hand, if it is higher than 14, it changes it to 1:

<script type="text/javascript">

var currentDate = new Date();
var witnessCurrentDate = new Date();

var quantityOfDays = (currentDate.getDay() < 15)? 28: 1;


document.write("The current date is: " + witnessCurrentDate.getMonth() + "/" + witnessCurrentDate.getDate() + ". After changing its day of the month to " + quantityOfDays + ", the resulting date is: " + currentDate.getMonth() + "/" + currentDate.getDate());


Technical Details

The setDate method belongs to the Date object, it is available since the version 1.0 of JavaScript, and it is supported in all major browsers.

JavaScript Manual >> Date Object >> setDate Method

© Copyright 2010-2018 ROM Cartridge

Books to Read and Listen to Online