JavaScript check if time is between 9am and 6pm?
Gulshan Saini
July 02, 2020
1 min

Suppose you want to show a message to the user on the website or run any NodeJS job only when the time is between two hours of a day.

To achieve this we can easily figure out if the current hour is between two hours or not using JavaScript Date() object and take the appropriate action.

Date class will return the hours in military time using method getHours() - 6 pm is represented as 18 in military time.


You could even extend this to minutes by using getMinutes() method and further check if minutes are also in a specified range or not.


Previous Article
How to conditionally render a component in React?

Subscribe to our newsletter!

We'll send you the best of our blog just once a month. We promise.

Related Posts

How to Get Current Date in Human Readable Form in JavaScript?
October 03, 2020
1 min
© 2020, All Rights Reserved.

Quick Links

Advertise with usContact Us

Social Media