changing body class based on user's local time
- by John
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?