Javascript if time is between 7pm and 7am do this? Javascript if time is between 7pm and 7am do this? javascript javascript

Javascript if time is between 7pm and 7am do this?


var today = new Date().getHours();if (today >= 7 && today <= 19) {   document.body.style.background = "Red";} else {    document.body.style.background = "Blue";}

See fiddle.


I suggest using a class on the body to manage the style, but handle the classes in JavaScript.

Essentially you'll use the Date class to get the current hour in military time (24 hour). 7 PM is represented as 19 in military time.

var hour = new Date().getHours();// between 7 PM and 7 AM respectivelyif(hour >= 19 || hour <= 7) {    document.body.className += 'between7';} else {    document.body.className += 'notBetween7';}

Then in CSS you can handle those classes.

body.between7 {    background-color: green;}body.notBetween7 {    background-color: red;}


Here is JSBin

var currentTime = new Date().getHours();if (currentTime >= 19 && currentTime <= 7) {   document.body.style.background = "/*your X color*/";} else {    document.body.style.background = "/*your Y color*/";}