A simple example of the size of JS date comparison

Source: Internet
Author: User
Tags comparison split time and date

  This article is mainly on the JS date comparison of a simple example of the introduction of the needs of friends can come to the reference, I hope to help you all

1, compare the date size of the JS code   code as follows: <script> var begindate=$ ("#beginDate"). Val ();    var enddate=$ ("#endDate"). Val ();    var D1 = new Date (Begindate.replace (/-/g, "/"));    var d2 = new Date (Enddate.replace (/-/g, "/"));       if (begindate!= "" &&enddate!= "" &&d1 >=d2)    {    alert (" Start time cannot be greater than end time! ");     return false;   &nbsp  </script>   2,JS compare time and date size code as follows: <html>    <head>    < title>calendar-Compare time and date sizes-www.jbxue.com</title>    <script type= "Text/javascript" src= " Calendarframework.js ></script>    </head>    <body>    start time:< Input onfocus= "Setday (This)" id= "StartTime" name= "StartTime" > to      end time: <input onfocus= "Setday" ( This) "id=" Endtime "name=" Endtime ">              <input type=" button "onclick=" C Hecktime ()"Value=" Check "/>              </body>    <script type=" Text/javasc Ript ">    checktime = function () {       var starttime = document.getElementById (" Start Time "). Value;        var endtime = document.getElementById ("Endtime"). Value;        Comparecalendar (StartTime, Endtime);         }   //Compare days before size   function comparedate (checkstartdate, checkenddate) {&nbs P         var arys1= new Array ();           var arys2= new Array ();       if (checkstartdate!= null && checkenddate!= null) {          ARYS1 =checkstartdate.split ('-');             var sdate=new Date (Arys1[0],parseint (arys1[1]-1), arys1[2]);           arys2=checkenddate.split ('-');           VAR edaTe=new Date (Arys2[0],parseint (arys2[1]-1), arys2[2]);       if (Sdate > Edate) {          alert ("Date start time greater than end time");              return false;         }  else {       alert ("through");        return True           {      }      }       //judgment date , Time size   function comparetime (StartDate, EndDate) {    if (startdate.length > 0 && enddate. Length > 0) {       var startdatetemp = Startdate.split ("");        var enddatetemp = Enddate.split ("");          var arrstartdate = Startdatetemp[0].split ("-");        var arrenddate = Enddatetemp[0].split ("-");          var arrstarttime = Startdatetemp[1].split (":");      &nbsP var arrendtime = Enddatetemp[1].split (":");      var allstartdate = new Date (arrstartdate[0], arrstartdate[1], arrstartdate[2], arrstarttime[0], ARRSTARTTIME[1], arrstarttime[2]);    var allenddate = new Date (arrenddate[0], arrenddate[1], arrenddate[2], arrendtime[0], arrendtime[1], ARRENDTIME[2]);      if (allstartdate.gettime () >= allenddate.gettime ()) {           Aler T ("StartTime cannot be greater than Endtime, cannot pass");            return false;   } else {       alert ("StartTime less than Endtime, so passed");        return True ;           }   } else {       alert ("Time cannot be empty");    & nbsp   return false;         }   }   //Comparison date, time size   function Comparecalendar (startdate, end Date) {   if (Startdate.indexof ("")!=-1 && EndDate.indexof ("")!=-1 {      //inclusive time, date           Comparetime (StartDate, EndDate);               } else {      //No time included, only date     &NB Sp Comparedate (StartDate, EndDate);         }    {   </script>    </html>   

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.