Using date-nfs
, how can I check if new Date()
is between 8am and 8pm?
Asked
Active
Viewed 541 times
-3

Dimitri Kopriwa
- 13,139
- 27
- 98
- 204
-
1Please at least [attempt to try to solve the issue on your own](http://idownvotedbecau.se/noattempt/). – Heretic Monkey Feb 17 '20 at 20:50
-
1`nighttime = new Date(); nightTime.setHours(20, 0, 0); isBefore(new Date(), nighttime)`? You've linked to the correct doc already, so what's the problem? – VLAZ Feb 17 '20 at 20:50
-
The title and the content of your question are a bit misleading? Are you trying to determine whether it's 8pm or 8am (day time or night time as the title indicates), or are you trying to determine if the current time is between the hours of 8am and 8pm (as the question text indicates)? – mhodges Feb 17 '20 at 20:50
-
var result = format(new Date(2014, 6, 2, 15), "h") ; if(result > 8 && result < 20) {//do your stuff} – Uthistran Selvaraj Feb 17 '20 at 20:55
2 Answers
0
You don't need to use external libraries, you can simply get the current hour by new Date().getHours()
, and check if it's more then 20 or less then 8.

Vlad Gincher
- 1,052
- 11
- 20
0
getHours()
on the date object will be an integer between 0 and 23. So you could check if it's between 8 and 20

andy mccullough
- 9,070
- 6
- 32
- 55