javascript conditions and storage in variables

0

good afternoon I have a question as I can do to save the format of one hour in a variable I mean I have the following function:

eventClick: function(event, jsEvent, view){
//creo un evento para extraer los datos de la base de dato y mostrarlos en el modal
var hora1=04:00:00;// almaceno dos horas que necesito comparar 
var hora2=07:00:00;
//extraigo la informacion de la columna seleccionada
var date_start = $.fullCalendar.moment(event.start).format('YYYY-MM-DD');
var time_start = $.fullCalendar.moment(event.start).format('hh:mm:ss');

$time_start= $('#modal-event #_time_start').val(time_start);
//almaceno la  informacion en una variable que time_start para luego compararla con las variables anterior mencionadas

$('#modal-event #delete').attr('data-id', event.id);
$('#modal-event #_title').val(event.title);
$('#modal-event #_placa').val(event.placa);
$('#modal-event #_date_start').val(date_start);
$('#modal-event #_time_start').val(time_start);

if ($time_start >  hora1 && $time_start  < hora2 ) {
   alert('pedido invalido debe Modificar la hora');
}else{
   alert('Pedido Valido');
}
 console.log(time_start);
 $('#modal-event').modal('show');     
 }

But the question is that it always takes the wrong variable not if it is the condition or is the way in which I store the time in the variable:

    
asked by Danier Perdomo 01.03.2018 в 20:14
source

2 answers

0

JavaScript does not have an object type for time, in fact when using

var hora1=04:00:00

This generates an error. Example when using the above in Stack Snippet generated the error "unexpected token:"

var hora1=04:00:00;
console.info(typeof hora1);

This is because in JavaScript the colon is used to assign labels ( label ) to sentences as well as in the switch statement to delimit the values to compare the expression ( case valor: ).

Instead of

var hora1=04:00:00;

could you use

var hora1='04:00:00';

And then extract each part or from the beginning use a variable for each component, hours, minutes and seconds.

    
answered by 01.03.2018 в 21:50
0

the solution for the question was to convert the variable into a string that was extracting from the database to then be able to compare it with an hour that was assigned as shown below

 var hora1 = '04:00:00';
 var hora2 = '07:00:00';

var time_start = $.fullCalendar.moment(event.start).format('hh:mm:ss');

var f = time_start.toString();

to then be able to make the condition and compare the two string

if (f > hora1 && f < hora2 ) {
   alert('pedido invalido debe Modificar la hora');
   console.log(f, "es mayor a", hora1);
}else{
   alert('Pedido Valido');
   console.log(f, "es menor a", hora1);
}
    
answered by 02.03.2018 в 14:24