私はここで潜在的な答えを見ましたが、それはYYYY-MM-DD: JavaScriptの日付検証のためのものでした。
上記のコードをMM-DD-YYYYのように修正しましたが、まだうまくいきません。
String.prototype.isValidDate = function()
{
var IsoDateRe = new RegExp("^([0-9]{2})-([0-9]{2})-([0-9]{4})$");
var matches = IsoDateRe.exec(this);
if (!matches) return false;
var composedDate = new Date(matches[3], (matches[1] - 1), matches[2]);
return ((composedDate.getMonth() == (matches[1] - 1)) &&
(composedDate.getDate() == matches[2]) &&
(composedDate.getFullYear() == matches[3]));
}
どうしたら、上記のコードをMM-DD-YYYY、さらにはMM/DD/YYYYに対応させることができますか?
ありがとうございます。
何がうまくいかないのでしょうか? テスト版です。
String.prototype.isValidDate = function() {
var match = this.match(/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/);
var test = new Date(match[3], match[1] - 1, match[2]);
return (
(test.getMonth() == match[1] - 1) &&
(test.getDate() == match[2]) &&
(test.getFullYear() == match[3])
);
}
var date = '12/08/1984'; // Date() is 'Sat Dec 08 1984 00:00:00 GMT-0800 (PST)'
alert(date.isValidDate() ); // true