How to get the number of days in a specified month using JavaScript ?
Given a month and the task is to determine the number of days of that month using JavaScript.
-
JavaScript getDate() Method: This method returns the number of days in a month (from 1 to 31) for the defined date.
Syntax:
Date.getDate()
Return value: It returns a number, from 1 to 31, representing the day of the month.
- JavaScript getFullYear() Method: This method returns the year (four digits for dates between year 1000 and 9999) of the defined date.
Syntax:
Date.getFullYear()
Return value: It returns a number, representing the year of the defined date
- JavaScript getMonth() Method: This method returns the month (from 0 to 11) for the defined date, based on to local time.
Syntax:
Date.getMonth()
Return value: It returns a number, from 0 to 11, representing the month.
Example 1: This example gets the days in the month (february) of the year (2020) by passing the month (1-12) and year to the function daysInMonth.
<!DOCTYPE HTML> <html> <head> <title> Get number of days in a specified month </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 15px; font-weight: bold;"> </p> <button onclick = "GFG_Fun()"> click here </button> <p id = "GFG_DOWN" style = "color:green; font-size: 20px; font-weight: bold;"> </p> <script> var up = document.getElementById('GFG_UP'); up.innerHTML = "Click on button to get the" + " number of days in specified month"; var down = document.getElementById('GFG_DOWN'); function daysInMonth (month, year) { return new Date(year, month, 0).getDate(); } function GFG_Fun() { var date = new Date(); var month = 2; var year = 2020; down.innerHTML = "Number of days in " + month + "nd month of the year " + year +" is "+ daysInMonth(month, year); } </script> </body> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Example 2: This example gets the days in the current month of the current year by passing the month (1-12) and year to the function daysInMonth.
<!DOCTYPE HTML> <html> <head> <title> Get number of days in a specified month </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 15px; font-weight: bold;"> </p> <button onclick = "GFG_Fun()"> click here </button> <p id = "GFG_DOWN" style = "color:green; font-size: 20px; font-weight: bold;"> </p> <script> var up = document.getElementById('GFG_UP'); up.innerHTML = "Click on button to get the number" + " of days in specified month"; var down = document.getElementById('GFG_DOWN'); function daysInMonth (month, year) { return new Date(year, month, 0).getDate(); } function GFG_Fun() { var date = new Date(); var month = date.getMonth() + 1; var year = date.getFullYear(); down.innerHTML = "Number of days in " + month + "th month of the year " + year +" is "+ daysInMonth(month, year); } </script> </body> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- How to calculate the number of days between two dates in javascript?
- Subtract days from Date object in JavaScript
- JavaScript | Adding days in milliseconds to Date object
- How to get the first and last date of current month using JavaScript ?
- How to get Month and Date of JavaScript in two digit format ?
- Program to find the number of days between two dates in PHP
- How to convert number to month name in PHP?
- How to add days to $Date in PHP?
- JavaScript | Number.MAX_VALUE & Number.MIN_VALUE with Examples
- p5.js | month() function
- How to convert a float number to the whole number in JavaScript?
- HTML | DOM Input Month min Property
- HTML | <input type="month">
- HTML | DOM Input Month name Property
- HTML | DOM Input Month max Property
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



