Work with hour and minute

-1

I would like to make a code that takes hour and minute so that I can work with a scheduling schedule for the execution of an action, for example:

var inserirProgramAtual = document.getElementById("CBNestudio");

    if ((new Date().getDay() > 0) && (new Date().getDay() < 6)) { 
        if (new Date().getHours() >= 5 && new Date().getHours() <= 9) {
            inserirProgramAtual.innerHTML = '

        <span>Jornal da manhã</span>

    '
        } else if (new Date().getHours() >= 9 && (new Date().getHours() <= 10)) {
            inserirProgramAtual.innerHTML = '

        <span>Programa qualquer</span>

    '

I managed to pick up time normally, which is 9 o'clock, but what could I do to catch 30 minutes? For example, I want you to take action at 9:10 or 9:30.

    
asked by anonymous 07.12.2018 / 18:11

1 answer

0

If it is to only not execute after the time, make the verification in two steps, as below:

let dateTime = new Date();

if(dateTime.getHours() >= 5 && dateTime.getHours() <= 9){
	if(dateTime.getHours() === 9 && dateTime.getMinutes() >= 30){
    console.log('vai parar');
  	return false
  }
  
  console.log('Continua...');
}

Now if you need to fire the script at the exact time, I recommend seeing about using cron job.

    
07.12.2018 / 19:08