Im wanting to apply different CSS sheets to my site depending on the time of the browser. e.g if its day time, display day.css or night.css for night.
I can do this with PHP but it is based on the server time, not the browsers local time.
Is there a way to tell the time in javascript? I'm probably going to use jQuery to simply initiate the css once its worked out.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With