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

160
Visualizações
How to get timezone abbreviation from given timestamp?

I'm running into an issue while testing two functions that convert ISO and Epoch timestamp to the client timezone. The tests are passing but I'm trying to modify them so we don't have to keep changing them based on Daylight time.

I added a bit of code to obtain the timezone abbreviation using Date() and toLocaleTimeString() and it works, except it fails in the CI/CD pipeline as the server returns UTC time. Even though, I'm adding options to specifically set the zone to America/Chicago, the server doesn't seem to recognize them.

describe('convertISO8601ToClientTimezone', () => {
  const timestamp = '2021-04-08T19:15:42.410506Z';
  const zoneAbbr = new Date(timestamp)
    .toLocaleTimeString('en-us', { timeZone: "America/Chicago", timeZoneName: 'short' })
    .split(' ')[2];

  it('should take ISO-8601 return date formatted string of local timezone offset accordingly', () => {
    expect(convertISO8601ToClientTimezone(timestamp)).toEqual(
      `April 8th 2021, 2:15:42 PM ${zoneAbbr}`
    );
  });
});

describe('convertEpochTimestampToClientTimezone', () => {
  const timestamp = 1617909517024179200; // epoch in nanoseconds
  const zoneAbbr = new Date(timestamp / 100000)
    .toLocaleTimeString('en-us', { timeZoneName: 'short' })
    .split(' ')[2];

  it('should take epoch timestamp and return date formatted string of local timezone offset accordingly', () => {
    expect(convertEpochTimestampToClientTimezone(timestamp)).toEqual(
      `April 8th 2021, 2:18:37 PM ${zoneAbbr}`
    );
  });
});

Is there a better way to get the timezone abbreviation for America/Chicago based on a given timestamp?

I'm also using the Moment library but I'm struggling to figure out how get the value needed from the mock fn. And actually, I'm not sure that would be the recommended approach here but I'm pretty new to Jest.

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

0

You can use moment-timezone library https://momentjs.com/timezone/

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