Change CSS based on time of the day

Submitted by:David Villa

Date added:16 February, 2016

Category:JavaScript

Use this in the <head> of your HTML files.

Tags: css based time

Code Snippet:

    <link rel="stylesheet" type="text/css" media="screen, projection" href="screen.css" />
<script type="text/javascript">
<!--
var d = new Date();
var sunrise = 6; // 6 AM
var sunset = 18; // 6 PM
var currentHour = parseInt(d.getHours());
if(currentHour >= sunset || currentHour <= sunrise){
document.write('<link rel="stylesheet" href="dark.css" media="screen, projection" /' + '>');
}
// -->
</script>
 
 

Comments