How to change the background color of the window depending on the time?
In this article you will see how to change the background color of the window depend on time. The background color will be change automatically depend on time. We use the Date object to display the time. Date object contains many methods like as follow:
- getDate(): It returns the date of the month.
- getDay(): It returns the day of the week.
- getSeconds(): It returns the seconds.
- getMinutes(): It returns the minutes.
- getHours(): It returns the hours.
- getTime(): It returns the complete time.
Here we will use only getHours() method like as follows:
var hours = now.getHours();
For Example: You will see the different background color of the window depend on time. In this example Color will change according to conditional statement. This example also shows the current time.
HTMLPage1.htm:
<html>
<head>
<title>Change time depend on time</title>
</head>
<body>
<font color=navy>
<h2>
Background color of this window will be change depend on time.
</h2>
</font>
<script type="text/javascript">
var now = new Date();
var hours = now.getHours();
//1-4 am day
if (hours >= 1 && hours < 5)
{
document.write('<body bgcolor="red" text="black">')
document.write("<h3>Please don't disturb me:</h3><p>"+"Red color shows the time between 1 am to 4 am. <p>"+"This color will change after 4 am")
}
//5-9 am
if (hours > 4 && hours < 10)
{
document.write('<body bgcolor="green" text="red">')
document.write("<h3>Good Morning:</h3><p>" +"Green color shows the time between 5 am to 9 am. <p>"+"This color will change after 9 am")
}
//10am -15 pm
if (hours >9 && hours < 16)
{
document.write('<body bgcolor="blue" text="red">')
document.write("<h3>Have a nice day:</h3><p>" +"Blue color shows the time between 10 am to 3 pm. <p>"+"This color will change after 3 pm");
}
//4 pm 8 pm
if (hours > 15 && hours < 21)
{
document.write('<body bgcolor="orange" text="white">')
document.write("<h3>Good Evening:</h3><p>" +" Orange color shows the time between 4pm to 8pm.<p>"+"This color will change after 8 pm")
}
//9pm - 10 pm
if (hours >= 21 && hours < 23)
{
document.write('<body bgcolor="yellow" text="red">')
document.write("<h3>Take Dinner:</h3><p>" +"Yellow color shows the time between 9 pm to 10 pm. <p>"+"This color will change after 10 pm")
}
//11 pm-12 am night
if (hours >= 23 && hours <24)
{
document.write('<body bgcolor="olive" text="white">')
document.write("<h3>Go to Sleep:</h3><p> " +"Olive color shows the time between 11 pm to 12 am. <p>"+"This color will change after 12 am")
}
</script>
<a href="#" onclick="location='HTMLPage2.htm'">
<font color=Black>
<h2>Click here to see the time</h2>
</font>
</a>
</body>
</html>
HTMLPage2.htm: To see the current time you can add the following script:
<html>
<head><title>Show current time</title>
<style>
.styling
{
background-color:"#ffffcc";
color:navy;
font: 20px MS Sans Serif;
padding: 6px;
}
</style>
</head>
<body bgcolor="#cc9900">Current Time is: <br /><br />
<span id="digitalclock" class="styling"></span>
<script>
var alternate=0
var standardbrowser=!document.all&&!document.getElementById
if (standardbrowser)
document.write('<form name="form1"><input type="text" name="text1" size="11"></form>')
function show()
{
if (!standardbrowser)
var clockobj=document.getElementById? document.getElementById("digitalclock") : document.all.digitalclock
var Digital=new Date()
var hours=Digital.getHours()
var minutes=Digital.getMinutes()
var dn="AM"
if (hours==12) dn="PM"
if (hours>12)
{
dn="PM"
hours=hours-12
}
if (hours==0) hours=12
if (hours.toString().length==1)
hours="0"+hours
if (minutes<=9)
minutes="0"+minutes
if (standardbrowser)
{
if (alternate==0)
document.form1.text1.value=hours+" : "+minutes+" "+dn
else
document.form1.text1.value=hours+" "+minutes+" "+dn
}
else
{
if (alternate==0)
clockobj.innerHTML=hours+"<font color='#ff0033'> : </font>"+minutes+" "+"<sup style='font-size:2px'>"+dn+"</sup>"
else
clockobj.innerHTML=hours+"<font color='blue'> : </font>"
+minutes+" "+"<sup style='font-size:2px'>"+dn+"</sup>"
}
alternate=(alternate==0)? 1 : 0
setTimeout("show()",1000)
}
window.onload=show()
</script>
</body>
</html>
Output: Output of the HTMLPage1.htm is as follows:
Figure 1: This figure show the time between 4 pm to 8 pm at this time.
To see the current time click on the given link. You will see the current time as follows:

Figure 2: Current time (Output of the HTMLPage2).
Conclusion:
Background color of the window will be change depend on time. Suppose the window background color is orange from 4 pm to 8 pm. It will be change at 9 pm when you click on refresh button.