I want to add change css using current time. this is the Ilustration the code i want.
var today = new Date();
var hourNow = today.getHours();
var style; //add css//
if (hourNow > 0) { //hour 00:00 - 12:00 the css//
document.getElementsByTagName('BODY')[0].style.backgroundColor = "#fff";
document.getElementsByTagName('BODY')[0].style.color= "#505050"; }
else if (hourNow > 12) { //hour 120:00 - 18:00 the css//
document.getElementsByTagName('BODY')[0].style.backgroundColor = "gainsboro";
document.getElementsByTagName('BODY')[0].style.color= "black"; }
else if (hourNow > 18) { //hour 18:00 - 00:00 the css//
document.getElementsByTagName('BODY')[0].style.backgroundColor = "#1b1b1b";
document.getElementsByTagName('BODY')[0].style.color= "#fff"; }
else {
document.getElementsByTagName('BODY')[0].style.backgroundColor = "white";
document.getElementsByTagName('BODY')[0].style.color= "black";}
is that possible? i am new with JavaScript..
document.getElementsByTagName('BODY')[0]can be much more simply written asdocument.body.}at the end of the last line of the block is not vaguely standard or readable (or maintainable).if elseinstead of multipleifelseand set it to black on white. @FastSnail's suggestion of usingelse ifwould solve that. If that's not the issue with your code, perhaps this is related.