Created
August 28, 2012 12:46
-
-
Save niwaringo/3497728 to your computer and use it in GitHub Desktop.
javascriptでのDateオブジェクト作成時のバリデート
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 isValidDate(date_str) { | |
var date = new Date(date_str), | |
date_reg = /^(\d{4}|\d{2})(?:\x2d|\u002f)(\d{2}|\d)(?:\x2d|\u002f)(\d{2}|\d)/, | |
valid = function() { | |
var date_strs = date_str.match(date_reg), | |
addDateStr = {}; | |
addDateStr.date = function() { | |
return (date.getFullYear() - 0) + (date.getMonth() - 0) + (date.getDate() - 0); | |
}; | |
addDateStr.str = function() { | |
return (date_strs[1] - 0) + (date_strs[2] - 1) + (date_strs[3] - 0); | |
}; | |
if (isNaN(date.getTime()) || !date_strs) return false; | |
return addDateStr.date() === addDateStr.str(); | |
}; | |
if (Object.prototype.toString.call(date) !== "[object Date]") return false; | |
return valid(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment