Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if it is day or night in Javascript? [duplicate]

Tags:

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.

like image 874
willdanceforfun Avatar asked Feb 12 '10 06:02

willdanceforfun


2 Answers

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().

like image 50
o.k.w Avatar answered Sep 28 '22 03:09

o.k.w


I use this logic:

const hours = new Date().getHours()
const isDayTime = hours > 6 && hours < 20
like image 32
Damian Pavlica Avatar answered Sep 28 '22 04:09

Damian Pavlica