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

190
Visualizações
MomentJS - Showing the wrong time for Pacific Timezone

I did not write the code involving Moment. But I'm tying to convert it to plain JavaScript.

moment.tz.add("America/Los_Angeles|PST PDT PWT PPT|80 70 70 70|010102301010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010|-261q0 1nX0 11B0 1nX0 SgN0 8x10 iy0 5Wp1 1VaX 3dA0 WM0 1qM0 11A0 1o00 11A0 1o00 11A0 1o00 11A0 1o00 11A0 1qM0 11A0 1o00 11A0 1o00 11A0 1o00 11A0 1o00 11A0 1qM0 WM0 1qM0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1fA0 1a00 1fA0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1cN0 1cL0 1cN0 1cL0 s10 1Vz0 LB0 1BX0 1cN0 1fz0 1a10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1a10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 14p0 1lb0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 14p0 1lb0 14p0 1lb0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 14p0 1lb0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0|15e6");
console.log('Central Time: ', moment()._d.toString());
console.log('Pacific Time: ', moment().tz("America/Los_Angeles")._d.toString());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.4/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.14/moment-timezone.min.js"></script>

I live in the Central Time Zone. As you can see above, MomentJS returns the correct time for the Central Timezone.

However, when I try to get the time in the Pacific Time Zone, it is 5 hours off. I suspect it has to do with the GMT difference, as shown here.

GMT-0500 (Central Daylight Time)

I have looked through the documentation and studied this extensively but cannot figure out why the time is 5 hours off.

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

0

I think the key point here is to avoid using the internal _d variable.

See Moment Internal Properties for details.

These values will not give the expected result:

In particular if Moment TimeZone is in use, this property will almost never be the same as the actual value that Moment will output from its public .format() function.

I would suggest using the .toString() or format() functions to output the date value.

You can also use the Date.toLocaleString() function to output a date in a given timezone in plain JavaScript.

// Moment
console.log('Moment:');
console.log('Local Time: ', moment().toString());
console.log('Central Time: ', moment().tz("America/Chicago").toString());
console.log('Pacific Time: ', moment().tz("America/Los_Angeles").toString());

// Plain JavaScript
let opts = { 
  weekday: 'short', month: 'short', day: 'numeric',
  hour: 'numeric', minute: 'numeric', second: 'numeric',
  timeZoneName: 'short' 
}

console.log('\nPlain JS:');
console.log('Local Time: ', new Date().toLocaleString('default', opts));
console.log('Central Time: ', new Date().toLocaleString('default', { ...opts, timeZone: 'America/Chicago' })); 
console.log('Pacific Time: ', new Date().toLocaleString('default', { ...opts, timeZone: 'America/Los_Angeles' })); 
.as-console-wrapper { max-height: 100% !important; }
<script src="https://momentjs.com/downloads/moment.js"></script>
<script src="https://momentjs.com/downloads/moment-timezone-with-data.js"></script>

about 4 years ago · Juan Pablo Isaza Relatório

0

Without moment, you can convert your timezone like below

// I live in a country with GMT+03:00 timezone
console.log(new Date()) //outputs: Fri Jul 08 2022 15:53:06 GMT+0300 (GMT+03:00)
console.log(new Date().toLocaleString('en-US', {timeZone: 'America/New_York'})) //outputs: "7/8/2022, 8:53:06 AM"

With moment

var date = moment("2014-12-01T12:00:00Z");
date.tz('America/New_York').format('ha z');  // outputs: 5am PDT

Further reading

Moment 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