Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Oct 2012
    Location
    mother land --india
    Posts
    165
    Thanks
    38
    Thanked 2 Times in 2 Posts

    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

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,011
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    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
    Last edited by Philip M; 02-05-2013 at 04:03 PM.

    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.

  • #3
    Regular Coder
    Join Date
    Oct 2012
    Location
    mother land --india
    Posts
    165
    Thanks
    38
    Thanked 2 Times in 2 Posts
    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

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,011
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    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, "/");
    Last edited by Philip M; 02-06-2013 at 04:09 PM. Reason: Typo

    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.

  • #5
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,086
    Thanks
    38
    Thanked 498 Times in 492 Posts

    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •