Skip to content

Instantly share code, notes, and snippets.

@luanvuhlu
Created April 5, 2017 06:34
Show Gist options
  • Save luanvuhlu/8d1b6365b1c5bc6cec26005b2e45f47a to your computer and use it in GitHub Desktop.
Save luanvuhlu/8d1b6365b1c5bc6cec26005b2e45f47a to your computer and use it in GitHub Desktop.
Validate date format yyyy/MM/dd
function checkDate(dateString){
if(!/^\d{4}\/\d{1,2}\/\d{1,2}$/.test(dateString))
return false;
var parts = dateString.split("/");
var year = parseInt(parts[0], 10);
var month = parseInt(parts[1], 10);
var day = parseInt(parts[2], 10);
console.log(parts)
if(year < 1000 || year > 3000 || month == 0 || month > 12)
return false;
var monthLength = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
monthLength[1] = 29;
return day > 0 && day <= monthLength[month - 1];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment