How to use setDate in JavaScript

In this article, I will explain use of setDate() method in JavaScript date object.
  • 1855

JavaScript setDate() method

setDate() method of date obeject sets the day of the month for a given date according to local time.

Syntax

Date.setDate(dayValue)

  • dayValue is an integer from 1 to 31, representing the day of the month.

  • If dayValue is 0. it will result in the last hour of the previous month
  • If dayValue is -1. it will result in the hour before the last hour of the previous month

Example

In the following example show to setDate() method sets the day of the month.

<!DOCTYPE html>

<html>

<body>

<h3 style="color:Teal ">JavaScript setDate() Method Example</h3>

<p id="demo">Click the button to display a specified date after changing the day of the month.</p>

<button onclick="myFunction()">Try it</button>

<script type="text/javascript">

    function myFunction() {

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

        d.setDate(15);

        var x = document.getElementById("demo");

        x.innerHTML = "Privious Date is : July 21, 1983 01:15:00 <br> After Set date is : " + d;

        }

</script>

</body>

</html>

Output1

setDate 1.jpg

Output2

setDate 2.jpg

You may also want to read these related articles here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.