Simple Example of JS date comparison size
This article mainly introduces simple instances with relatively large JS dates. If you need some help, please refer to them.
1. Compare the js Code of date size
The Code is 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 ("the start time cannot be later than the end time! ");
Return false;
}
</Script>
2. Js compares the time and date size
The Code is as follows:
<Html>
<Head>
<Title> calendar-compare the time and date size -www.jbxue.com </title>
<Script type = "text/javascript" src = "CalendarFramework. js"> </script>
</Head>
<Body>
Start Time: <input onfocus = "setday (this)" id = "startTime" name = "startTime">
End Time: <input onfocus = "setday (this)" id = "endTime" name = "endTime">
<Input type = "button" onclick = "checkTime ()" value = "check"/>
</Body>
<Script type = "text/javascript">
CheckTime = function (){
Var startTime = document. getElementById ("startTime"). value;
Var endTime = document. getElementById ("endTime"). value;
CompareCalendar (startTime, endTime );
}
// Compare the date size
Function compareDate (checkStartDate, checkEndDate ){
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 ("the date start time is later than the end time ");
Return false;
} Else {
Alert ("pass ");
Return true;
}
}
}
// Judge the date and time
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, cannot pass ");
Return false;
} Else {
Alert ("startTime is earlier than endTime, so it passes ");
Return true;
}
} Else {
Alert ("time cannot be blank ");
Return false;
}
}
// Compare the date and time
Function compareCalendar (startDate, endDate ){
If (startDate. indexOf ("")! =-1 & endDate. indexOf ("")! =-1 ){
// Contains the time and date
CompareTime (startDate, endDate );
} Else {
// Does not contain time, only contains date
CompareDate (startDate, endDate );
}
}
</Script>
</Html>