The setDate() Method

The setDate() method sets the day of the month (1-31). The setDate() method can also be used to add days to a date:

HTML file: Displayed by browser:
<body>
<h4>The setDate() method sets the date of the current month:</h4>
<p id="demo"></p>
<h4>Adding 45 days to the above date:</h4>
<p id="demo2"></p>
<script>
var d = new Date();
d.setDate(15);
document.getElementById("demo").innerHTML = d;
var add = new Date();
add.setDate(d.getDate() + 45);
document.getElementById("demo2").innerHTML = add;
</script>
</body>

The setDate() method sets the date of the current month:

Adding 45 days to the above date:

If adding days, shifts the month or year, the changes are handled automatically by the Date object.

BackTable of ContentsNext