Skip to content

Instantly share code, notes, and snippets.

@kamlekar
Created July 16, 2015 10:44
Show Gist options
  • Save kamlekar/cebaa23ea45ba7959022 to your computer and use it in GitHub Desktop.
Save kamlekar/cebaa23ea45ba7959022 to your computer and use it in GitHub Desktop.
Check whether date is valid or not
function getDate(time) {
// Refer: http://stackoverflow.com/a/10589791/1577396
// Refer: http://stackoverflow.com/a/1353711/1577396
if(!time) {
return false;
}
var dateTime = new Date(time);
// Valid date
if(Object.prototype.toString.call(dateTime) === "[object Date]" && !isNaN(dateTime.getTime())){
return dateTime;
}
// Invalid date (The above condition will be invalid for some time formats in firefox)
else{
// Refer: http://stackoverflow.com/a/3075893/1577396
var t = time.split(/[- :]/);
try {
return new Date(t[0], t[1] - 1, t[2], t[3], t[4], t[5]);
}
catch(ex) {
// Invalid date
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment