ziad eson ziad eson - 6 months ago 10
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

<script>..</script>
else do another script.

I tried this code:

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















Hope my question is clear.

Answer

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; }