<strong>1,比较日期大小的js代码<br></strong><div class="codetitle"> <span><a style="CURSOR: pointer" data="15038" class="copybut" id="copybut15038" onclick="doCopy('code15038')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code15038"> <br><script><BR>var beginDate=$("#beginDate").val(); <BR> var endDate=$("#endDate").val(); <BR> var d1 = new Date(beginDate.replace(/\-/g, "\/")); <BR> var d2 = new Date(endDate.replace(/\-/g, "\/")); <br><br> if(beginDate!=""&&endDate!=""&&d1 >=d2) <BR> { <BR> alert("开始时间不能大于结束时间!"); <BR> return false; <BR> }<BR> </script><br> </div> <br><strong>2,Js比较时间和日期大小<br></strong><div class="codetitle"> <span><a style="CURSOR: pointer" data="6752" class="copybut" id="copybut6752" onclick="doCopy('code6752')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code6752"> <br> <br> <br><title>calendar-比较时间和日期大小-www.jb51.net</title> <br><script type="text/javascript" src="CalendarFramework.js"></script> <br> <br> <br>开始时间:<input onfocus="setday(this)" id="startTime" name="startTime">到 <br>结束时间:<input onfocus="setday(this)" id="endTime" name="endTime"> <br> <input type="button" onclick="checkTime()" value="校验"> <br> <br><script type="text/javascript"> <BR>checkTime = function() { <BR> var startTime = document.getElementById("startTime").value; <BR> var endTime = document.getElementById("endTime").value; <BR> compareCalendar(startTime, endTime); <BR>} <BR>//比较日前大小 <BR>function compareDate(checkStartDate, checkEndDate) { <BR> var arys1= new Array(); <BR> var arys2= new Array(); <BR>if(checkStartDate != null && checkEndDate != null) { <BR> arys1=checkStartDate.split('-'); <BR> var sdate=new Date(arys1[0],parseInt(arys1[1]-1),arys1[2]); <BR> arys2=checkEndDate.split('-'); <BR> var edate=new Date(arys2[0],parseInt(arys2[1]-1),arys2[2]); <BR>if(sdate > edate) { <BR> alert("日期开始时间大于结束时间"); <BR> return false; <BR>} else { <BR> alert("通过"); <BR> return true; <BR> } <BR> } <BR>} <br><br>//判断日期,时间大小 <BR>function compareTime(startDate, endDate) { <BR> if (startDate.length > 0 && endDate.length > 0) { <BR> var startDateTemp = startDate.split(" "); <BR> var endDateTemp = endDate.split(" "); <br><br> var arrStartDate = startDateTemp[0].split("-"); <BR> var arrEndDate = endDateTemp[0].split("-"); <br><br> var arrStartTime = startDateTemp[1].split(":"); <BR> var arrEndTime = endDateTemp[1].split(":"); <br><br>var allStartDate = new Date(arrStartDate[0], arrStartDate[1], arrStartDate[2], arrStartTime[0], arrStartTime[1], arrStartTime[2]); <BR>var allEndDate = new Date(arrEndDate[0], arrEndDate[1], arrEndDate[2], arrEndTime[0], arrEndTime[1], arrEndTime[2]); <br><br>if (allStartDate.getTime() >= allEndDate.getTime()) { <BR> alert("startTime不能大于endTime,不能通过"); <BR> return false; <BR>} else { <BR> alert("startTime小于endTime,所以通过了"); <BR> return true; <BR> } <BR>} else { <BR> alert("时间不能为空"); <BR> return false; <BR> } <BR>} <BR>//比较日期,时间大小 <BR>function compareCalendar(startDate, endDate) { <BR>if (startDate.indexOf(" ") != -1 && endDate.indexOf(" ") != -1 ) { <BR> //包含时间,日期 <BR> compareTime(startDate, endDate); <BR>} else { <BR> //不包含时间,只包含日期 <BR> compareDate(startDate, endDate); <BR> } <BR>} <BR></script> <br> <br> </div>