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
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
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
Philip,
Thanks but i am using Jquery plugin for datetime selection, link is below.
PHP Code:
http://trentrichardson.com/examples/timepicker/
And I have a 40 fields use these plugin and they are sequential displayed means one after other
now, I want a Javascript plugin which creates a rule for all the 40 date time fields like, B's datetime should be great then A, C's datetime should be great then D, D's datetime should be great then E and so on........
Please help me....By using above code I need to create 40 Var's that is more legacy in spite can I have a class which can differentiate field with its ID.
Simply pass the fields to the function for each comparison:-
Code:
function validate(firstdatefield, seconddatefield) {
var s = document.getElementById(firstdatefield).value;
s = s.replace(/[\s\-\:]/g, "/");
var e = document.getElementById(seconddatefield).value;
e = e.replace(/[\s\-\:]/g, "/");
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Last edited by Philip M; 02-06-2013 at 04:09 PM..
Reason: Typo
Simplified alterntive version with assumed input from user or function...
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title> Untitled </title>
<script type="text/javascript">
Date.prototype.toDate = function(dinfo) { // assumes dinfo format is "MM/DD/YYYY hh:mm" as military time [hh:mm]
if ((dinfo=='') || (dinfo==undefined)) { return new Date(); }
else {
var tmp = dinfo.split(' ');
var tarr = tmp[1].split(':');
var darr = tmp[0].split(/\/|\.|\-|\_/);
return new Date(darr[2], darr[0]-1, darr[1], tarr[0], tarr[1], 0);
}
}
</script>
</head>
<body>
Assuming results of "datetime" picker are:<p>
<input id="Asced" value="02/06/2013 10:20"> Schedule A<p>
<input id="Bsced" value="02/05/2013 12:30"> Schedule B<p>
<input id="Csced" value="02/04/2013 14:40"> Schedule C<p>
<input id="Dsced" value="02/03/2013 16:30"> Schedule D<p>
<button id="checkSced" onclick="checkSchedules()">Check Schedules</button>
<script type="text/javascript">
function checkSchedules() {
var a = new Date().toDate(document.getElementById('Asced').value);
var b = new Date().toDate(document.getElementById('Bsced').value);
var c = new Date().toDate(document.getElementById('Csced').value);
var d = new Date().toDate(document.getElementById('Dsced').value);
var status = true;
if ((a < b) || (b < c) || (c < d)) { status = false; }
alert('Valid date sequence: '+status+'\n\n'+a+'\n'+b+'\n'+c+'\n'+d); // for testing purposes only
return status;
}
</script>
</body>
</html>