Pure JavaScript Determines whether the query date is a valid date _javascript tips

Source: Internet
Author: User

The following content mainly through the JS code to introduce you, the code is relatively simple, including comments, there are good suggestions welcome.

The following figure, when a query condition contains a date, such as "2012-3-4", verifies that the date string entered before the query is a valid date

var snapshot_createtime_begin=$ (selector+ "input[name= ' Createtime_begin ')"). Val (). Trim ();
      var snapshot_createtime_end=$ (selector + "Input[name= ' Createtime_end ')"). Val (). Trim (); try{//Determines whether the start time is a valid date if (snapshot_createtime_begin!= "" &&new date (snapshot_createtime_begin). Getdat
        E ()!=snapshot_createtime_begin.match (/-\d{0,2}$/g) [0].replace (/-/g, "")) {throw new Error (); //Determine if the end time is a valid date if (snapshot_createtime_end!= "" &&new date (snapshot_createtime_end). GetDate ()!=sn
        Apshot_createtime_end.match (/-\d{0,2}$/g) [0].replace (/-/g, "")) {throw new Error (); } if (Date.parse (snapshot_createtime_begin) >date.parse (snapshot_createtime_end)) {//alert ("Start date should not exceed end Date!
          "); Alert ("The start date should not exceed the end date!")
          ");
        return; } $.extend (pageobj,{createtimebegin:snapshot_createtime_begin, Createtimeend:snapshot_createt
        Ime_end,}); InitpaginatioN (); }catch (e) {//alert ("Please enter a valid date!") Alert ("Please enter a valid date!")
      "); }

JS to determine whether the date is a valid day

function IsDate (intyear,intmonth,intday) { 
 if isNaN (intyear) | | isNaN (intmonth) | | isNaN (Intday)) return false;   
 if (intmonth>12| | INTMONTH<1) return false; 
 if (intday<1| | INTDAY>31) return false; 
 if (intmonth==4| | intmonth==6| | intmonth==9| | intmonth==11) && (intday>30)) return false; 
 if (intmonth==2) { 
   if (intday>29) return false;  
   if (((intyear%100==0) && (intyear%400!=0)) | | (intyear%4!=0)) && (intday>28)) return false; 
  }
 return true; 
}

The above code is the validity of the date check, I hope to help you.

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.