How to determine if it is day or night in javascript or jquery?

var hr = (new Date()).getHours(); //get hours of the day in 24Hr format (0-23)

Depending on your definition of day/night, perform your magic :)

PS: If your day/night starts not at the exact hour, you can try getMinutes().


I use this logic:

const hours = new Date().getHours()
const isDayTime = hours > 6 && hours < 20

(new Date).getHours()

will get the local hour of the time (0-23) of the client. Based on that value, swap the stylesheet for the page. I would set the day stylesheet as the default and swap it out when necessary.

My initial thought is that you would want to perform this operation as soon as possible on the client to avoid any potential browser reflow.