Created
April 5, 2019 07:11
-
-
Save vella-nicholas/b509cead818d47e6f2e09dde26014f31 to your computer and use it in GitHub Desktop.
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
/** | |
* How to check if a date is valid with vanilla JavaScript | |
* @link https://gomakethings.com/how-to-check-if-a-date-is-valid-with-vanilla-javascript/ | |
*/ | |
/** | |
* Get the number of days in any particular month | |
* @link https://stackoverflow.com/a/1433119/1293256 | |
* @param {integer} m The month (valid: 0-11) | |
* @param {integer} y The year | |
* @return {integer} The number of days in the month | |
*/ | |
var daysInMonth = function (m, y) { | |
switch (m) { | |
case 1 : | |
return (y % 4 == 0 && y % 100) || y % 400 == 0 ? 29 : 28; | |
case 8 : case 3 : case 5 : case 10 : | |
return 30; | |
default : | |
return 31 | |
} | |
}; | |
/** | |
* Check if a date is valid | |
* @link https://stackoverflow.com/a/1433119/1293256 | |
* @param {[type]} d The day | |
* @param {[type]} m The month | |
* @param {[type]} y The year | |
* @return {Boolean} Returns true if valid | |
*/ | |
var isValidDate = function (d, m, y) { | |
m = parseInt(m, 10) - 1; | |
return m >= 0 && m < 12 && d > 0 && d <= daysInMonth(m, y); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment