Home >Web Front-end >JS Tutorial >A simple example of JS date comparison size_javascript skills

A simple example of JS date comparison size_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:04:131283browse
1. JS code for comparing date sizes
Copy code The code is as follows:

<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("The start time cannot be greater than the end time!"); <br> return false; <br> }<br> </ script><br> </div> <br><strong>2, Js compare time and date size<br></strong><div class="codetitle"> <span><a style="CURSOR: pointer" data="6752" class="copybut" id="copybut6752" onclick="doCopy('code6752')"><u>Copy code</u></a></span>The code is as follows:</div> <div class="codebody" id="code6752"> <br><html> <br><head> <br><title>calendar-Compare time and date size-www.jb51.net</title> text/javascript" src="CalendarFramework.js"></script>


Start time: To  
End time:   
   
                                                                                {
var startTime = document.getElementById("startTime").value;
var endTime = document.getElementById("endTime").value;
compareCalendar(startTime, endTime);
} // Compare the size
Function Comparedate (CheckstartDate, Checkenddate) {
var arys1 = new array ();
var arys2 = new array ();
if (CheckstartDate! = Null && checkEndDate != null) {                                                                                                                                                           
arys2=checkEndDate.split('-'); var edate=new Date(arys2[0],parseInt(arys2[1]-1),arys2[2]);
if(sdate > edit) { > } 
 } 


//Judge the date and 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(":");
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()) {
alert("startTime cannot be greater than endTime and cannot be passed");
return false;
} else {
alert(" startTime is less than endTime, so it passed");
return true;
}
} else {
alert("The time cannot be empty");
return false;
}
}
//Compare dates and times
function compareCalendar(startDate, endDate) {
if (startDate.indexOf(" ") != -1 && endDate.indexOf(" ") != -1 ) {
//Includes time, date
compareTime(startDate, endDate); ;
} }
}



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn