Get the time to open based on the day

0

I have the following HTML code

Sunday 18:00 - 23:30
Monday 18:00 - 23:30
Tuesday 18:00 - 23:30
Wednesday 18:00 - 23:30
Thursday 18:00 - 23:30
Friday 18:00 - 23:30
Saturday 18:00 - 23:30

In The Javascript I have:

var hour_start = 14;
var hour_end = 15;

function comprobarHora(fecha){
  var time = fecha.getHours();
  alert(fecha.toLocaleTimeString()
    + (time>=hour_start && time<hour_end ? ': abierto' : ': cerrado'));
}
var d = new Date();
comprobarHora(d);

What I could not achieve, is to make the javascript check today, and take the time slot that is shown in the html.

an example:

If today is Monday and it is 18:00, you must show an alert that it is open.

Because in the html Monday = 18:00 a 23:30 .

Any help or suggestion?

Thank you very much!

    
asked by Juan David 06.12.2017 в 13:23
source

1 answer

1

I understand that the problem is that each day can have a different schedule.

What you should do is create an array with the different times of each day of the week. In my example I have set a same time from Monday to Friday (3:00 pm - 11:30 pm), another for Saturday (8:00 pm to 11:30 pm) and another for Sunday (00:00 - 00:00, then closed).

The value of the time, both start and end, I set it in number of minutes: the hour by 60 plus the minutes.

I modified the time checking function so that it selects the correct time according to the day of the week (obtained with getDay ) and calculating the number of minutes of the date in the same way: hours per 60 more minutes.

var horarios = [
  {start: 0, end: 0 }, // domingo (no abre)
  {start: 15*60, end: 23*60 + 30}, // lunes (15:00-23:30)
  {start: 15*60, end: 23*60 + 30}, // martes (15:00-23:30)
  {start: 15*60, end: 23*60 + 30}, // miércoles (15:00-23:30)
  {start: 15*60, end: 23*60 + 30}, // jueves (15:00-23:30)
  {start: 15*60, end: 23*60 + 30}, // viernes (15:00-23:30)
  {start: 20*60, end: 23*60 + 30}, // sábado (20:00-23:30)
];

function comprobarHora(fecha){
  var day = fecha.getDay();
  var time = fecha.getHours() * 60 + fecha.getMinutes();
  console.log(fecha.toLocaleTimeString()
    + (time>=horarios[day].start && time<horarios[day].end ? ': abierto' : ': cerrado'));
}
var d = new Date(); // Día y hora actual
comprobarHora(d);
d = new Date(2017, 11, 6, 15, 38, 0); // Miércoles 15:38
comprobarHora(d);
d = new Date(2017, 11, 9, 15, 38, 0); // Sábado 15:38
comprobarHora(d);
d = new Date(2017, 11, 9, 21, 38, 0); // Sábado 21:38
comprobarHora(d);

To be able to indicate as closing time at 12 o'clock at night, you can indicate the time in seconds and set the closing time at 23:59:59.

In order to have several sections on the same day we could modify the objects of the schedules by adding the day they refer to.

In this example to see if it is open, I use the some method to check if any of the time bands meet the specified conditions (which corresponds to the day and time is between the start and the end):

var horarios = [
  {day: 0, start: 0, end: 0 }, // domingo (no abre)
  {day: 1, start: 09*60*60, end: 12*60*60}, // lunes (09:00:00-12:00:00)
  {day: 1, start: 14*60*60, end: 22*60*60}, // lunes (14:00:00-22:00:00)
  {day: 2, start: 15*60*60, end: (23*60 + 30)*60}, // martes (15:00:00-23:30:00)
  {day: 3, start: 15*60*60, end: (23*60 + 30)*60}, // miércoles (15:00:00-23:30:00)
  {day: 4, start: 15*60*60, end: (23*60 + 30)*60}, // jueves (15:00:00-23:30:00)
  {day: 5, start: 15*60*60, end: (23*60 + 30)*60}, // viernes (15:00:00-23:30:00)
  {day: 6, start: 20*60*60, end: (23*60 + 59)*60 + 59}, // sábado (20:00:00-23:59:59)
];

var weekdays = ['domingo', 'lunes', 'martes', 'miércoles', 'jueves', 'viernes', 'sábado'];

function comprobarHora(fecha){
  var day = fecha.getDay();
  var time = (fecha.getHours() * 60 + fecha.getMinutes())*60 + fecha.getSeconds();
  console.log(weekdays[day] + ' ' +
    fecha.toLocaleTimeString() +
    (horarios.some(t=> t.day===day && time>=t.start && time<t.end) ? ': abierto' : ': cerrado'));
}
var d = new Date(); // Día y hora actual
comprobarHora(d);
d = new Date(2017, 11, 6, 15, 38, 0); // Miércoles 15:38
comprobarHora(d);
d = new Date(2017, 11, 9, 15, 38, 0); // Sábado 15:38
comprobarHora(d);
d = new Date(2017, 11, 9, 23, 59, 58); // Sábado 23:59:58
comprobarHora(d);
d = new Date(2017, 11, 18, 08, 52, 0); // Lunes 08:52:00
comprobarHora(d);
d = new Date(2017, 11, 18, 11, 0, 0); // Lunes 11:00:00
comprobarHora(d);
d = new Date(2017, 11, 18, 13, 0, 0); // Lunes 13:00:00
comprobarHora(d);
d = new Date(2017, 11, 18, 17, 0, 0); // Lunes 17:00:00
comprobarHora(d);
    
answered by 06.12.2017 / 13:50
source