Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-05-2013, 01:40 PM   PM User | #1
nani_nisha06
Regular Coder

 
Join Date: Oct 2012
Location: mother land --india
Posts: 159
Thanks: 37
Thanked 2 Times in 2 Posts
nani_nisha06 is an unknown quantity at this point
Need java script to check if datetime is greater than earlier datetime

Hi frnds,


I am back, this time I need you support on checking if datetime is greater then earlier datetime.

My requirement.

I have a form which has 4 datetime fields A,B,C & D and I want to validate them before I send the values to DB.

I want to check if A's datetime is > B's datetime is > C's datetime is > D's datetime field.

Please help me.

Regards,
Nani
nani_nisha06 is offline   Reply With Quote
Old 02-05-2013, 03:54 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,041
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.

Last edited by Philip M; 02-05-2013 at 04:03 PM..
Philip M is online now   Reply With Quote
Old 02-06-2013, 01:26 PM   PM User | #3
nani_nisha06
Regular Coder

 
Join Date: Oct 2012
Location: mother land --india
Posts: 159
Thanks: 37
Thanked 2 Times in 2 Posts
nani_nisha06 is an unknown quantity at this point
Quote:
Originally Posted by Philip M View Post
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.

Regards,
Nani
nani_nisha06 is offline   Reply With Quote
Old 02-06-2013, 04:02 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,041
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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
Philip M is online now   Reply With Quote
Old 02-07-2013, 04:19 AM   PM User | #5
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,764
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Lightbulb

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>
Change "checkSchedules()" results as necessary
jmrker is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:03 PM.


Advertisement
Log in to turn off these ads.