locked
Compare two dates and two hours in JavaScript RRS feed

  • Question

  • User1692641958 posted

    Hello friends I want to compare two hours one that is value obtained from an id, and the other built with the current time and two date also one that is value obtained from an id, and the other built with the current date where:

    var HoraActual = $('input:text[id=COMD_FECHA]').val();//=> 10:04 AM

    var FechaActual = $('input:text[id=COMD_FECHA_ATENCION]').val();//=> 09/10/2018

    var fa = fe + "/" + (f.getMonth() + 1) + "/" + f.getFullYear();//=> 09/10/2018

    var strTime = hours + ':' + minutes + ' ' + ampm;//=> 10:04 AM

            var HoraActual = $('input:text[id=COMD_FECHA]').val();
            var FechaActual = $('input:text[id=COMD_FECHA_ATENCION]').val();
            var f = new Date();
            var fe = "";
            if (f.getDate() < 10) {
                fe = "0" + f.getDate();
            } else {
                fe = f.getDate();
            }
            var fa = fe + "/" + (f.getMonth() + 1) + "/" + f.getFullYear();
            var hours = f.getHours();
            var minutes = f.getMinutes();
            var ampm = hours >= 12 ? 'pm' : 'am';
            hours = hours % 12;
            hours = hours ? hours : 12; // the hour '0' should be '12'
            minutes = minutes < 10 ? '0' + minutes : minutes;
            var strTime = hours + ':' + minutes + ' ' + ampm;
    
            if (FechaActual >== fa && HoraActual >== strTime ) {
                alert("Fecha y Hora actual es mayor");
            }
            else {
                alert("no son iguales");
            }

    Tuesday, October 9, 2018 3:26 PM

All replies

  • User475983607 posted

    The code is comparing strings not numeric values.  Convert the values to proper Date types or use an API like moment.js.

    https://momentjs.com/

    Tuesday, October 9, 2018 4:32 PM
  • User839733648 posted

    Hi RockChris,

    According to your description and code, it seems that you are comparing strings.

    I think thay you have to convert them to dates first and then compare.

    For more about Date Constructor in javascript, you may refer to the link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date 

    I've made a sample and test on my side, you could refer to it.

        <script type="text/javascript">
            var today = new Date();
            var dd = today.getDate();
            var mm = today.getMonth() + 1;//January is 0
            var yyyy = today.getFullYear();
            if (dd < 10) {
                dd = '0' + dd
            }
            if (mm < 10) {
                mm = '0' + mm
            }
            todayresult = dd + '/' + mm + '/' + yyyy;
            console.log(today);
            console.log(todayresult);
            var datestr = "18/10/2018";
            var parts = datestr.split("/");
            var dateinput = new Date(parts[2], parts[1] - 1, parts[0]);
            var dd1 = dateinput.getDate();
            var mm1 = dateinput.getMonth() + 1;
            var yyyy1 = dateinput.getFullYear();
            if (dd1 < 10) {
                dd1 = '0' + dd1
            }
            if (mm1 < 10) {
                mm1 = '0' + mm1
            }
            dateresult = dd1 + '/' + mm1 + '/' + yyyy1;
            console.log(dateresult);
            console.log(dateinput);
            console.log(todayresult === dateinput);
        </script>

    output:

    Best regards,

    Jenifer

    Thursday, October 18, 2018 1:27 AM