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

157
Visualizações
Setting date and timezone is not working in ReactJS

I have a ReactJs project where also i use jest for testing. To display the time according UK timezone, i created this function:

 const getDateTZ = dt => {
  const d = new Date(dt)
  const o = {
    hour: '2-digit',
    minute: '2-digit',
    timeZone: 'Europe/London'
  }
  return d.toLocaleTimeString('en-GB', o)
}
console.log(getDateTZ(new Date()))

This function work fine and running it i get the right time. To test this function with jest i add this:

test('It should return time', () => {
  const today = new Date()
  today.setHours(11)
  today.setMinutes(30)
  const r = getDateTZ(today)
  expect(r).toEqual('09:30')
})
Note: The difference between my location and UK is 2 hours, UK is with 2 hours behind. The test passed. So running the test alone without running all tests it passes.
ISSUE: To run all tests from entire project in my package json i added this task: "test": "TZ=UTC jest the test above fails and output this when i do npm run test:

Expected: "09:30"
Received: "11:30"

So when i run all the tests my test fails because i receive 11:30 instead of 09:30 even if the test works if i run it separately in the test file.
Question: Why i get this issue and how to get rid of it?

about 4 years ago · Juan Pablo Isaza
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