Work with hour and minute

Asked

Viewed 224 times

-1

I would like to make a code that takes time and minute so that I can work with a schedule for the execution of an action, 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 the schedule normally, which is 9 o'clock, but what could I do to get 30 minutes? For example, I want at 9:10 or 9:30 to perform an action.

  • 1

    Please explain more clearly your question.

  • 2

    It’s not just calling getMinutes? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getMinutes

  • I want the code action to run at 9:30 am, I’ve tried with getMinutes and it didn’t work

  • 3

    In this case, I agree with Wictor that it is not clear what you want and how you are doing it. Please click [Edit] and explain in more detail what you want to do and what do you mean "it didn’t work" (gave some error? the code behaved unexpectedly? what? etc). In other words, try to make a [mcve]

  • I edited, in case it still doesn’t help I try to be clearer, sorry the question misspelled.

  • Are you thinking the information from where? And what do you want to do with this information?

  • The focus is the code line: if (new Date().getHours() >= 5 && new Date().getHours() <= 9) {...} .

  • 1

    Okay, so the script would be triggered at exactly 9:30. Who would keep an eye on the clock and finger on the mouse button to run the script at the exact moment the time is 9:30? Easier to make parole if the current time and minute is greater than 9:30 am

Show 3 more comments

1 answer

0


If only to not perform after the time do the two-step verification 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.