JavaScript code
/// Check whether the input date is in the correct date format:
/// Supports four input formats: yyyy-M-d, yyyy-MM-dd, yyyy/M/d, and yyyy/MM/dd.
Function checkDate (strInputDate ){
// Define a constant array of months and days
Var DA = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31];
// Unified Date Format
StrDate = strInputDate. replace (/-/g ,"/");
// Determine whether the date is in the expected format
If (strDate. indexOf ("/") =-1 ){
Alert ("Enter the format yyyy-M-d, yyyy-MM-dd, yyyy/M/d, and yyyy/MM/dd. ")
Return false;
}
// Year, month, and day
ArrD = strDate. split ("/");
If (arrD. length! = 3) return false;
Y = parseInt (arrD [0], 10 );
M = parseInt (arrD [1], 10 );
D = parseInt (arrD [2], 10 );
// Determine whether the value is a number.
If (isNaN (y) | isNaN (m) | isNaN (d) return false;
// Determine whether the month is between 1 and 12
If (m> 12 | m <1) return false;
// Determine whether it is a leap year
If (isLoopYear (y) DA [2] = 29;
// Determine whether the input date exceeds the total number of days of the month.
If (d> DA [m]) return false;
// If all the conditions are verified, it should be a valid date.
// If You Want to format the date once, you can process it here. The following format is the date format recognized by the database: yyyy-MM-dd.
// Str = y + "-" + (m <10? "0": "") + m + "-" + (d <10? "0": "") + d;
Str = y + "-" + (m <10? "0": "") + m + "-" + (d <10? "0": "") + d;
Alert (str)
Return true;
}
Function isLoopYear (theYear ){
Return (new Date (theYear, 1, 29). getDate () = 29 );
}
// Method 2:
/// Check whether the input date is in the correct date format:
/// Supports four input formats: yyyy-M-d, yyyy-MM-dd, yyyy/M/d, and yyyy/MM/dd.
Function CheckDate2 (strInputDate ){
If (strInputDate = "") return false;
StrInputDate = strInputDate. replace (/-/g ,"/");
Var d = new Date (strInputDate );
If (isNaN (d) return false;
Var arr = strInputDate. split ("/");
Return (parseInt (arr [0], 10) = d. getFullYear () & (parseInt (arr [1], 10) = (d. getMonth () + 1) & (parseInt (arr [2], 10) = d. getDate ()));
}