Not sure what you mean by "datetime". This will validate/compare two dates. You can of course extend the comparison to four dates.
Here you are:-
Code:
<html>
<head>
</head>
<body>
Enter the starting date (DD/MM/YYYY) <input type = "text" id = "start"><br>
Enter the ending date (DD/MM/YYYY) <input type = "text" id = "end"><br>
<input type = "button" value = "Check Dates" onclick = "validate()">
<script type = "text/javascript">
function validate() {
var s = document.getElementById("start").value;
s = s.replace(/[\s\-\:]/g, "/");
var e = document.getElementById("end").value;
e = e.replace(/[\s\-\:]/g, "/")
var ss = s.split("/");
var es = e.split("/");
var sv = checkValidDate(ss[2],ss[1],ss[0]);
if (sv) {
var ev = checkValidDate(es[2],es[1],es[0]);
}
if (sv && ev) { // both dates valid
var nd1 = new Date(ss[2], ss[1]-1, ss[0]); // remember that in Javascript date objects the months are 0-11
var nd2 = new Date(es[2], es[1]-1, es[0]);
if (nd1 > nd2) { // check start not before end - change to >= if required
alert ("Invalid data - start date is after the end date!\nRe-enter the dates, please.");
document.getElementById("start").value = "";
document.getElementById("end").value = "";
return false;
}
}
}
function checkValidDate(yr,mmx,dd) {
if (yr <1910 || yr >2013) { // you may want to change 2013 to some other year!
alert ("Year is out of range");
return false;
}
mm = mmx-1; // remember that in Javascript date objects the months are 0-11
var nd = new Date();
nd.setFullYear(yr,mm,dd); // format YYYY,MM(0-11),DD
var ndmm = nd.getMonth();
if (ndmm != mm) {
alert (dd + "/" + mmx + "/" + yr + " is an Invalid Date!\nRe-enter the dates, please!");
document.getElementById("start").value = "";
document.getElementById("end").value = "";
return false;
}
else {
alert (dd + "/" + mmx + "/" + yr + " is a Valid Date");
return true;
}
}
</script>
</body>
</html>
If you wish to use US date format mm/dd/yyyy then you will have to adjust the above accordingly.
Another method would be to convert the four validated dates to milliseconds (epoch time) for the comparison, using setTime().
“Education is the process of casting imitation pearls before real swine” - Irwin Edman