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

396
Visualizações
How can I calculate the hours of difference between two timezones in nodeJS?

How can I calculate the difference between UTC (that I generate with new Date()) and European Standard time?

Something like

const amsterdam = new Date('Europe/Amsterdam')
amsterdam.getTimezoneOffset() // returns the minutes of offset in this case 60

I can't simply use 1 hour since the time shifts in the winter and summer! :(

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

0

You can get the offset for a particular location for any date using Intl.DateTimeFormat with suitable options, e.g.

/* @param {string} loc - IANA representative location
 * @param {Date} date - default to current date
 * @returns {string} offset as ±H[mm]
 */
function getOffsetForLoc(loc, date = new Date()) {
  // Use Intl.DateTimeFormat to get offset
  let opts = {hour: 'numeric', timeZone: loc, timeZoneName:'short'};
  let getOffset = lang =>  new Intl.DateTimeFormat(lang, opts)
    .formatToParts(date)
    .reduce((acc, part) => {
      acc[part.type] = part.value;
      return acc;
    }, {}).timeZoneName;
  let offset = getOffset('en');
  // If offset is an abbreviation, change language
  if (!/^UCT|GMT/.test(offset)) {
    offset = getOffset('fr');
  }
  // Remove GMT/UTC 
  return offset.substring(3);
}

// Get current offsets for following locations
['Europe/Amsterdam',
 'America/New_York',
 'Asia/Kolkata']
  .forEach(loc => console.log(`${loc} : ${getOffsetForLoc(loc)}`));

// Get offsets in Amsterdam
[new Date(2021,0),  // 1 Jan 2021
 new Date(2021,5)   // 1 Jun 2021
].forEach(d => console.log(`Offset for Amsterdam on ${d.toLocaleDateString()} ${getOffsetForLoc('Europe/Amsterdam', d)}`));

about 4 years ago · Juan Pablo Isaza Relatório

0

What I eventually ended up doing and what worked perfectly for me was the following

      const timezoneOffsetInHours =
      moment().tz('Europe/Amsterdam').hour() - new Date().getHours()

This returns the hours that Amsterdam is ahead of UTC.

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