Akkz
Akkz

Reputation: 13

How can I run a code between 9:00 AM to 10:00 AM using Javascript?

I need to run a code between 9 AM and 10 AM. The code should not run before 9 AM nor after 10 AM. But the code should stay active between these times. The code will be like changing the background colour of a div element.

How do I implement this in Javascript?

Upvotes: 1

Views: 104

Answers (1)

Dat Tran
Dat Tran

Reputation: 1586

I see you have react tag, So I assume that you want to render a div in react. You can use inline background color to set it.

This code return a div that have red color between 9AM and 10AM. Otherwise, It has blue color.

render() {
    const hour = new Date().getHours();
    const background = hour >= 9 && hour <= 10 ? 'red' : 'blue'
    return (
        <div style={{backgroundColor: background}}></div>
    )
}

Upvotes: 1

Related Questions