how to compare with javascript two datepicker fields

0

I need to compare two fields in my html that are with datepicker. Here is my code below:

    var inicio = $("#Periodo_De").datepicker("getDate");
    var final = $("#Periodo_Ate").datepicker("getDate");

    if (inicio > final) {
      console.log('inicio é maior que final');
    }
    
asked by anonymous 30.01.2015 / 15:22

1 answer

0

I think you should know that for datapicker to work you should add the libs of it ...

In this case, in addition to JQuery I am using the JQuery-UI library:

<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/smoothness/jquery-ui.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>

HTMLcode:

<inputtype="text" id="inicio" name="inicio">
<input type="text" id="final" name="final">

<input type="button" id="testar" name="testar" value="testar">

JS Code:

$(document).ready(function () { 
    $('#inicio').datepicker({ dateFormat: 'dd/mm/yy' }).val();
    $('#final').datepicker({ dateFormat: 'dd/mm/yy' }).val();
});

$("#testar").click(function() {
    var inicio = $('#inicio').val();
    var final = $('#final').val();

    if (inicio > final) {
        alert('inicio é maior que final');
    }else{
        alert('final é maior que inicio');
    }
});

Here in JsFiddle an example of datapicker working the way you need it ...

    
30.01.2015 / 16:01