To print the number of days in a given month, we can use the getDate() javascript function. This will returns the total days of the month according to local time.
getDate() function return the data in integar format from 1 to 31.
Syntax is:
1 |
Date.getDate() |
Javascript Function to Get Days in Month:
You can try below code to get a number of days in a specified month and year :−
In this code, we are passing two parameters in getDate() function. One is month value and second is year.
1 2 3 4 5 6 7 8 9 10 11 12 |
<!DOCTYPE html> <html> <body> <script> var NoOfDays = function(month,year) { return new Date(year, month, 0).getDate(); }; document.write("Days in April: "+NoOfDays(4, 2021)); // April month document.write("<br>Days in June: "+NoOfDays(6, 2021)); // June Month </script> </body> </html> |
As you see above javascript code will print the number of days of April and June month. We have passed two parameters to get the total number of days and getDate() returned the days value in integer format.