getDate() is an date method in JavaScript, which is used to return the day of the month (between 1 to 31) for the specified date.
This method does not have any parameters.
In JavaScript, by using getDate() we can return the date of the present day in the month.
<p>Click the below button to get Today's Date.</p> <input type="button" value="Get Date" onclick="myDate()" /> <p id="myId"></p> <script> function myDate() { var a = new Date(); var r = a.getDate(); document.getElementById("myId").innerHTML = r; } </script>
In the above code snippet we have given Id as "myId"to the second <p>
element in the HTML code. There is a function myDate() in the script code which is connected to the Onclick of the HTML button.
We need to return the date of the present day in the month, for that we are using getDate() method.
Onclick of the button "Get Date" in the HTML code fires the function myDate() in the script code at the same time the getDate() returns the date and gives the output. Where as new Date() is used to return the date, day, month, year, time.
OUTPUT
<p>Click the below button to get specified Date.</p> <input type="button" value="Get Date" onclick="myDate()" /> <p id="myId"></p> <script> function myDate() { var a = new Date("August 15, 1947 00:25:33"); var r = a.getDate(); document.getElementById("myId").innerHTML = r; } </script>
Returning the day with specified date
We are returning the day with the specified date by using getDate() method.
We specified the particular date in the new Date("August 15, 1947 00:25:33") method.
Onclick of the button returns the the only day (15) portion from the specified date as output.
OUTPUT
Views: 74357 | Post Order: 99