+ 2

J.S - Can we use switch on hours. I know we can on days. But how do u do it on hours?

Can we say something like if hour is 1:00 pm say "hi"? Using switch statement Can we use the switch statement to say something in each hour? If yes: Please show me how, with a code or on here.

21st Jun 2019, 9:49 PM
Ginfio
Ginfio - avatar
2 odpowiedzi
+ 3
Of course. You want to catch all times within that hour, right? function getHour() { return new Date().getHours(); // a value from 0 to 23 } switch (getHour()) { case 13: // corresponds with anywhere between 1pm and 1:59pm. // If you want 1am to 1:59am, use 1. alert("hi"); break; default: alert("Be patient. The hour will come eventually."); } If you want it to hit the hour case only when it is 1:00 to 1:01, you would just have to tweak things a bit.
21st Jun 2019, 11:15 PM
Josh Greig
Josh Greig - avatar
+ 1
Josh Greig , that's what i'm looking for, thanks.
22nd Jun 2019, 12:47 AM
Ginfio
Ginfio - avatar