Blue Theme Orange Theme Green Theme Red Theme
 
Nevron Chart
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
6 Months Free & No Setup Fees ASP.NET Hosting!
Search :       Advanced Search »
Home » JavaScript » How to: Change the background color of the window depends on the time

How to: Change the background color of the window depends on the time

This article shows you to change the background color of the window depend on time.

Author Rank :
Page Views : 8664
Downloads : 70
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
Wind_BGColor.zip
 
 
Team Foundation Server Hosting
Become a Sponsor
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

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'>&nbsp;:&nbsp;</font>"+minutes+" "+"<sup style='font-size:2px'>"+dn+"</sup>"

                else

                clockobj.innerHTML=hours+"<font color='blue'>&nbsp;:&nbsp;</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.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Purushottam Rathore

I am working as a Software Developer and has 4 years of experience on Microsoft Technology and having a Master Degree in Computer Application. I really like to work in the .NET platform. and working with ASP.NET 2.0/3.5, Web Services, WPF, WCF, Silverlight, AJAX, JavaScript, JQuery, Ado.net, MsAccess, SQL Server 2005/2008.

Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Comments
Nevron Chart
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.