Compare two dates and two hours in JavaScript

-1

Hello friends I want to compare two hours one that is value obtained from an id, and the other one 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");
}
    
asked by Chris Alexis 09.10.2018 в 17:07
source

2 answers

1

I recommend you use Moment
< script src="https://unpkg.com/moment" /> < script> moment.now() > moment(2016-11-23); //Regresa un boolean </script>

Where moment.Now is your current date and in the second moment (Date_to_comparate) you pass the date you get from your inputs. Greetings!

    
answered by 09.10.2018 в 18:27
0

The following example takes the current date and the date obtained from the input time and date inserts it into another Date object. separating the time to insert it with setHours and setMinutes . in setDate we add one more day for reasons of difference with the Date object and the input date.

var fecha_actual = new Date();
var timeArray = $('#hora').val().split(":");
var fecha_input = new Date($('#fecha').val());
fecha_input.setDate(fecha_input.getDate()+1);
fecha_input.setHours(timeArray[0]);

fecha_input.setMinutes(timeArray[1]);

if(fecha_actual > fecha_input){
  console.log("Fecha actual es mayor");
}else{
  console.log("Fecha actual es menor");
}
console.log(fecha_actual,fecha_input);
    
answered by 09.10.2018 в 18:21