JS to determine whether the time format is correct code

Source: Internet
Author: User

JS to determine whether the time format is correct code:

If the requirements are more stringent, the time format is also required to judge, the following is a code example, I hope to be able to bring some help to the needs of friends.

The code example is as follows:

functionisdatestring (strdate) {varStrseparator = "-"; varStrdatearray; varintyear; varIntmonth; varIntday; varboolleapyear; varErrorMsg = ""; Strdatearray=Strdate.split (Strseparator); if(Strdatearray.length! = 3) {errormsg+ = "Date format must be: YYYY-MM-DD"; returnerrormsg; } 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 date must be a pure number"; returnerrormsg; }   if(intmonth>12 | | intmonth<1) {errormsg+ = "Date format error: Month must be between 1 and 12"; returnerrormsg; }   if((intmonth==1| | intmonth==3| | intmonth==5| | intmonth==7 | | intmonth==8| |intmonth==10| | intmonth==12) && (intday>31| | Intday<1) ) {errormsg+ = "date format error: Otsuki must be between 1 and 31 days"; returnerrormsg; }   if((intmonth==4| | intmonth==6| | intmonth==9| | intmonth==11) && (intday>30| | Intday<1) ) {errormsg+ = "Date format error: The number of days in a Yue must be between 1 and 31"; returnerrormsg; }   if(intmonth==2)  {     if(Intday < 1) {errormsg+ = "Date format error: Date must be greater than or equal to 1"; returnerrormsg; } 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: February days of leap year cannot exceed 29"; returnerrormsg; }     }     Else    {       if(Intday > 28) {errormsg+ = "date format error: February days for non-leap years cannot exceed 28"; returnerrormsg; }     }   }   returnerrormsg;}

The original address is: http://www.51texiao.cn/javascriptjiaocheng/2015/0523/2309.html

The most original address is: http://www.softwhy.com/forum.php?mod=viewthread&tid=9318

JS to determine whether the time format is correct code

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.