Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

357
Views
daylight or night time with javascript

I want to have a website with different UIs based on if it is day or night in client's timezone. right now I have this code based on what I found on internet:

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

if (isDayTime === True){
    //do this...
}

but the problem is sunrise and sunset is different in each timezone, also it changes through seasons, so considering 6 to 20 as my day time hours is wrong(since it could be night time before 20 in winter).

Is there a way to get day time or night time based on client timezone?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

A particularly interesting question with a more complex answer than I think you may have anticipated.

First off, I fear you have also forgotten to consider that timezones alone do not provide enough information to calculate day or night. You must also cross-reference that with a latitude reference because the duration of day and night vary across that.

A helpful lead, from another answer on Stack Overflow with similar credentials: https://stackoverflow.com/a/15044683/60318

A concise description of an algorithm to calculate the sunrise and sunset is provided by the United States Naval Observatory, available here:

http://edwilliams.org/sunrise_sunset_algorithm.htm

In addition to providing the date and location, you also need to select a Zenith angle (at which the sun will be considered to have "risen" or "set") - the page linked has several options.

This would still need to be built into a function, but perhaps if you are clever with looking for gists on github you might find something helpful.

There is a sunrise/sunset calculator found here: https://gml.noaa.gov/grad/solcalc/sunrise.html

You can get an idea of cross-referencing the user's location with the timezones, and I think you will have a more complete solution.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!