Skip to content

Instantly share code, notes, and snippets.

@sampathsl
Created October 18, 2016 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sampathsl/6f4bb5c680735731bfbc4621b2697197 to your computer and use it in GitHub Desktop.
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
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