Created
April 30, 2019 23:00
-
-
Save chrisgfortes/e4136c4128619518c445cd8965bc2226 to your computer and use it in GitHub Desktop.
Get number of days in a specified month using JavaScript?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Month here is 1-indexed (January is 1, February is 2, etc). This is | |
// because we're using 0 as the day so that it returns the last day | |
// of the last month, so you have to add 1 to the month number | |
// so it returns the correct amount of days | |
function daysInMonth (month, year) { | |
return new Date(year, month, 0).getDate(); | |
} | |
// July | |
daysInMonth(7,2009); // 31 | |
// February | |
daysInMonth(2,2009); // 28 | |
daysInMonth(2,2008); // 29 | |
https://github.com/datejs/Datejs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment