changing body class based on user's local time

Posted by John on Stack Overflow See other posts from Stack Overflow or by John
Published on 2011-01-10T23:50:54Z Indexed on 2011/01/10 23:53 UTC
Read the original article Hit count: 136

Filed under:
|
|
|

I'm trying to add a body class of 'day' if it's 6am-5pm and 'night' if "else" based on the user's local time.

I tried the following but it didn't work. Any ideas?

In the head:

<script>
function setTimesStyles() {
var currentTime = new Date().getHours();
if(currentTime > 5 && currentTime < 17) {
document.body.className = 'day';
}
else {
document.body.className = 'night';
}
}
</script>

In the body:

<body onload="setTimeStyles();">

Also, is there a more elegant way to achieve what I need?

© Stack Overflow or respective owner

Related posts about time

Related posts about change