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

248
Visualizações
Using moment.js to check if local time is between 2 other time zones times

Similar questions have been asked but for whatever reason I just can't wrap my head around this. I want a user anywhere in the world to see whether a shop is open or closed on my site. The shop's hours are 8am-8pm M-F, and 8am-7pm Sat-Sun.

So basically the code needs to grab the users local time and compare that to the current time in CT, and see if the shop is open. This is made a bit more complicated by the fact that if its 7pmCT on Tuesday night, it could be Wednesday morning in a far eastern time zone, so now you need to compare the days too.

All the searching I did found bits and pieces of similar questions/answers but I just can't wrap my head around how to put it all together to come up with a solution. I started by trying this:

var currTimeTokyo = moment.tz(moment(), 'Asia/Tokyo').format();
var currTimeCT = moment.tz(moment(), 'America/Chicago').format();
console.log(currTimeTokyo);
console.log(currTimeCT);

and that gives:

2021-10-30T00:51:42+09:00
2021-10-29T10:51:42-05:00

Those are correct, but now how do I compare to today's open/closed hours? I need to check if the Tokyo time is between open and close in CT, but the open/close times change depending on what day it is, and Tokyo could be a different day from CT anyways. How would you account for all of that?

UPDATE: I found something that worked and posted as an answer.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I don't know moment.js and I think it is already announced deprecated (day.js or luxon would be current alternatives) so I can't give you the proper syntax, but I try to help with the logic.

  1. Like you said, take the current local time from the user
  2. convert it to CTtime the shop is in. And if there's tuesday, then there's tuesday and depending on your shop hours it's closed or open. It doesn't matter if the timezone you're checking from might already have changed the day
  3. compare the local time of the shop to the opening hours - which workday is it? Is it between opening and closing?
about 4 years ago · Juan Pablo Isaza Relatório

0

I looked into Luxon as Corrl suggested and I came up with this, which I think solves my problem.

// open hours are M-F 8am-8pm, Sat-Sun 8am-7pm (all times CT)
var hours = [ 
    { day: 'Sunday', open:8, close:19 },
    { day: 'Monday', open:8, close:20 },
    { day: 'Tuesday', open:8, close:20 }, 
    { day: 'Wednesday', open:8, close:20 },
    { day: 'Thursday', open:8, close:20 },
    { day: 'Friday', open:8, close:20 },
    { day: 'Saturday', open:8, close:19 }
];

var isOpen = false;
var local = luxon.DateTime.local(); // get current time in local tz
var localInCT = local.setZone("America/Chicago"); // rezone to CT without changing timestamp;

hours.forEach(function(item, index){
    if (index === localInCT.weekday && localInCT.hour >= item.open && localInCT.hour < item.close) {
        isOpen = true;
    }
});
console.log(isOpen);
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