ziad eson ziad eson - 1 year ago 58
Javascript Question

How can I run different scripts based on time?

I'm basic in java-scripts and HTML, So I want to ask: How can I run different scripts based on the time?,
like: from 6AM -to- 7PM, do

else do another script.

I tried this code:

< !–
theDate = new Date()
theHour = theDate.getHours()
if (theHour >= 7 && theHour <= 18) {
href = "#day"
} else {
href = "#night"

Hope my question is clear.

Answer Source

var hour = new Date().getHours(); // hour will get current hour like 0 to 23 switch(hour) { case '6': // code in this case will run if current time is in 6am to 7am alert('hi, it's in 6 a.m to 7 a.m now'); break; case '7': // any other hour // add your code you want to run between 7 - 8 am break; }

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download