Get the time with jquery

0

I have the following code, with this I can add a class (today) in a div. Now what I need is to be able to do the following,

18:00 - 20:00

I want to be able to calculate that if the time range is not between 18:00 y 20:00 , add a text even div that says (Closed).

var d = new Date();
var n = d.getDay();
n = n > 0 ? n - 1 : 6; // zero is sunday, not monday in javascript
$('li.week').eq(n).addClass('today');

UPDATE

Now I have the following.

var start = new Date('18:00').getTime();
var end = new Date('20:00').getTime();
var now = new Date().getTime();

if( (start < now )) {
  alert("opened");
}
else {
 alert("closed");
}

I only have one problem I can not find a way to get the time validated with another.

18:00 a 20:00

If you are not in that time range, you should say Closed.

    
asked by Juan David 06.12.2017 в 10:38
source

1 answer

2

You only have to use the getHours method to get the time of the date.

Something like this:

function comprobarHora(fecha){
  var hora = fecha.getHours();
  console.log(fecha.toLocaleTimeString()
    + (hora>=18 && hora<20 ? ': abierto' : ': cerrado'));
}

var d = new Date();
comprobarHora(d);
d = new Date(2017,12,6,18,54,23);
comprobarHora(d);
d = new Date(2017,12,6,20,17,21);
comprobarHora(d);
    
answered by 06.12.2017 / 11:00
source