[JavaScript]JavaScriptは日付フォーマットが正しいかどうかを判断します。

15802 ワード

自動更新http://blog.csdn.net/dallasnash/archive/2007/04/18/1569068.aspx
どれほどの変動はなく、主にエラー情報を返して、関数部分を呼び出してalertが出てきます。正規表現でチェックできるということですが、今度検討してみます。
転載者は少なくとも作者と出所を明記します。http://www.cnblogs.com/GuominQiu

  
    
// ---------------------------------------------------------------------------
//

//
,
function isDateString(strDate){
var strSeparator = " - " ; //
var strDateArray;
var intYear;
var intMonth;
var intDay;
var boolLeapYear;
var ErrorMsg = "" ; //
strDateArray = strDate.split(strSeparator);

// , 2008-8-8 //strDate.length != 10 ||
if (strDateArray.length != 3 ) {
ErrorMsg
+= " : yyyy-MM-dd " ;
return ErrorMsg;
}

intYear
= parseInt(strDateArray[ 0 ], 10 );
intMonth
= parseInt(strDateArray[ 1 ], 10 );
intDay
= parseInt(strDateArray[ 2 ], 10 );

if (isNaN(intYear) || isNaN(intMonth) || isNaN(intDay)) {
ErrorMsg
+= " : " ;
return ErrorMsg;
}

if (intMonth > 12 || intMonth < 1 ) {
ErrorMsg
+= " : 1 12 " ;
return ErrorMsg;
}

if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7
|| intMonth == 8 || intMonth == 10 || intMonth == 12 )
&& (intDay > 31 || intDay < 1 )) {
ErrorMsg
+= " : 1 31 " ;
return ErrorMsg;
}

if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11 )
&& (intDay > 30 || intDay < 1 )) {
ErrorMsg
+= " : 1 31 " ;
return ErrorMsg;
}

if (intMonth == 2 ){
if (intDay < 1 ) {
ErrorMsg
+= " : 1 " ;
return ErrorMsg;
}

boolLeapYear
= false ;
if ((intYear % 100 ) == 0 ){
if ((intYear % 400 ) == 0 )
boolLeapYear
= true ;
}
else {
if ((intYear % 4 ) == 0 )
boolLeapYear
= true ;
}

if (boolLeapYear){
if (intDay > 29 ) {
ErrorMsg
+= " : 2 29 " ;
return ErrorMsg;
}
}
else {
if (intDay > 28 ) {
ErrorMsg
+= " : 2 28 " ;
return ErrorMsg;
}
}
}

return ErrorMsg;
}