JavaScript code for determining whether the date format is correct

Source: Internet
Author: User

At least the author and source should be indicated for the streamer! Http://www.cnblogs.com/GuominQiu
Copy codeThe Code is as follows:
//---------------------------------------------------------------------------
// Determine whether the date format is correct
// The returned value is an error message, indicating a valid date string without an error message
Function isDateString (strDate ){
Var strSeparator = "-"; // Date Separator
Var strDateArray;
Var intYear;
Var intMonth;
Var intDay;
Var boolLeapYear;
Var ErrorMsg = ""; // error message
StrDateArray = strDate. split (strSeparator );
// The length is not determined. In fact, is also reasonable // strDate. length! = 10 |
If (strDateArray. length! = 3 ){
ErrorMsg + = "the date format must be 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 + = "Date Format error: year, month, and day must be pure numbers ";
Return ErrorMsg;
}
If (intMonth> 12 | intMonth <1 ){
ErrorMsg + = "Date Format error: month must be between 1 and 12 ";
Return ErrorMsg;
}
If (intMonth = 1 | intMonth = 3 | intMonth = 5 | intMonth = 7
| IntMonth = 8 | intMonth = 10 | intMonth = 12)
& (IntDay> 31 | intDay <1 )){
ErrorMsg + = "Date Format error: the number of days in a large month must be between 1 and 31 ";
Return ErrorMsg;
}
If (intMonth = 4 | intMonth = 6 | intMonth = 9 | intMonth = 11)
& (IntDay> 30 | intDay <1 )){
ErrorMsg + = "Date Format error: the number of days in a small month must be between 1 and 31 ";
Return ErrorMsg;
}
If (intMonth = 2 ){
If (intDay <1 ){
ErrorMsg + = "Date Format error: date must be greater than or equal to 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 + = "Date Format error: the number of days in March of a leap year cannot exceed 29 ";
Return ErrorMsg;
}
} Else {
If (intDay> 28 ){
ErrorMsg + = "Date Format error: the number of days in February of a non-leap year cannot exceed 28 ";
Return ErrorMsg;
}
}
}
Return ErrorMsg;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.