Created
October 18, 2016 03:08
-
-
Save sampathsl/6f4bb5c680735731bfbc4621b2697197 to your computer and use it in GitHub Desktop.
Validate the user entered date using JavaScript - buggy (UTC) - Gives invalid date for 01/01/1970
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
function checkDate(str) | |
{ | |
var matches = str.match(/(\d{1,2})[\/](\d{1,2})[\/](\d{4})/); | |
if (!matches) return; | |
var day = parseInt(matches[1],10); | |
var month = parseInt(matches[2],10); | |
var year = parseInt(matches[3],10); | |
var date = new Date(year, month - 1, day); | |
if (!date || !date.getTime()) return; | |
if (date.getMonth() + 1 != month || | |
date.getFullYear() != year || | |
date.getDate() != day) { | |
return; | |
} | |
return(date); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment