2

I have a form where I am fetching two Dates from input tag. I need to validate and compare a date should always be greater than the another date. Here is javascript below :

function validateDD(gateout_plnd_dt){
    var dt_pat = /^[0-9]{2}-[0-9]{2}-[0-9]{4}$/;
    var alpha = /^[a-zA-Z0-9\s\.\-]+$/
    var int = /^[0-9.]+$/;
    var gateout_plnd_dt = document.getElementsByName('gateout_plnd_dt[]');
    var gate_in_dt = document.getElementsByName('gate_in_dt[]');


    for(var i=0,j=0; i<gateout_plnd_dt.length,j<gate_in_dt.length; i++,j++){



        if(gateout_plnd_dt[i].value< gate_in_dt[j].value){
        alert((gateout_plnd_dt[i].value));
        alert(gate_in_dt[j].value);
         return false;
        }
        }


    }

I need to convert it from string into date object which i am unable to do so. Will be really helpful to get it done. Thanks.

2
  • 1
    Check out moment js.. It has parser based on date format Commented Dec 24, 2015 at 5:18
  • will it work in array? Commented Dec 24, 2015 at 5:33

3 Answers 3

1

Try this

for(var i=0; i<gateout_plnd_dt.length; i++){

    out_date = gateout_plnd_dt[i].split("-");
    in_date = gate_in_dt[i].split("-");
    gateout_date = new Date(out_date[1],out_date[0],out_date[2]).getTime()
    gatein_date = new Date(in_date[1],in_date[0],in_date[2]).getTime()

    if(gateout_date < gatein_date ){
    alert(gateout_plnd_dt[i]);
    alert(gate_in_dt[i]);
     return false;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Change the split(-) to split("-");@Preethi Mano
1

Try this i works for you.

var parts ='you string'.split("-");
var mydate = new Date(parts[2],parts[0],parts[1]); 

7 Comments

I have edited it let me know. If that works for you.@user3445158
it works for single date input form, but not in array of input with multiple date. on checikng in Inspect element in console section, it show "'you string'.split("-"); is not a function"
what value you will have in this. gateout_plnd_dt[i].value
As per my code i am entering multiple date though gateout_plnd_dt in mysql table.I already have a date value in gate_in_dt. So gateout_plnd_dt>gate_in_dt .
each element in the list is string right.@user3445158
|
0

convert date to timestamp. I wouldn't suggest using a library for that reason.

 function datetotimestamp(date){
    var date=date.split("-");
    var newd=date[1]+"/"+date[0]+"/"+date[2];
    return new Date(newd).getTime();
    }

console.log( datetotimestamp("26-02-2012") < datetotimestamp("25-02-2012")) //returns false

to compare two or more values in array, use an extra function

var compareinarray = function(array) {
    for(var i = 1; i < array.length; i++)    {
        if(datetotimestamp(array[i]) > datetotimestamp(array[i-1]))
            return false;
    }
    return true;
}

var arr = ["26-02-2012", "23-02-2012"];
var res = compareinarray(arr); 
console.log(res)

update the compareinarray function var, according to your case and array complexity.

1 Comment

This will work for single date input, but fails in array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.