Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

358
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda