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

167
Visualizações
Moment js time zone date conversion issue

I am using moment js to convert user system time to user specified time zone as per his preference. User system time zone is IST and he is specifing a different time zone. For setting the same I am using

moment.tz.setDefault("Pacific/Pago_Pago");

But in one of the scenario I am getting different dates. If I do

moment().startOf('day')

I am getting today's date as Thu Oct 28 2021 00:00:00 GMT+0530 (India Standard Time), But if I am doing

moment(endDate).startOf('day')

I am getting Thu Oct 27 2021 00:00:00 GMT+0530 (India Standard Time) Where end date is the result of last statement i.e. Thu Oct 28 2021 00:00:00 GMT+0530 (India Standard Time)

What I am doing wrong here.

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

0

The timezone used when a moment object is created stays as part of the object and is used for subsequent processing. The effect described will happen if the default timezone is changed after the initial moment object is created.

E.g.

// Set default to IST (+5:30)
moment.tz.setDefault('Asia/Kolkata');

// Create initial moment using default timezone
let start = moment().startOf('day');

// Changing the default here doesn't affect above moment
moment.tz.setDefault('Pacific/Pago_Pago');

// This still uses the initial +5:30 offset, not -11:00 for Pago Pago
let timestamp = start.toString();

// This uses the timestamp for the original timezone, but
// then gets start of day in new default timezone
let end = moment(timestamp).startOf('day');

console.log( `Start: ${start.toString()}\nEnd  : ${end.toString()}` );
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment-with-locales.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.31/moment-timezone-with-data-10-year-range.js"></script>

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