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

350
Visualizações
js increment date by 3 hours when i put exact time in date
const date = new Date(2022, 4, 18, 16, 50);

Why does this return the time reduced by three hours?

I get this "2022-05-18T13:50:00.000Z" instead of this "2022-05-18T16:50:00.000Z" which I expected to get when I print this to console using console.log(date).

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

0

why does this return the time reduced by three hours?

It doesn't, it gives you exactly the time you asked for — in your local time. But then you're getting a string from it in a way that gives you a string in UTC, not local time (the Z at the end tells you it's UTC). Apparently there's a three-hour difference as of that date/time between your local timezone and UTC.

If you want to work in local time, use toLocaleString to get a string formatted according to your current locale and using your timezone. If you want a string like the one you got, but in your local timezone, as far as I know you have to build it yourself.

If you want to work in UTC, build the date using the UTC function: new Date(Date.UTC(2022, 4, 18, 16, 50));

about 4 years ago · Juan Pablo Isaza Relatório

0

it's because of your timezone! for example:

let d = new Date(2022, 4, 18, 16, 50);
d.toLocaleString('en-US', { timeZone: 'America/New_York' });
about 4 years ago · Juan Pablo Isaza Relatório

0

When you call the Date() constructor with individual components it will interpret those in your local time. See point 5 on this MDN documentation page.

But when you print int without toLocaleString() it will return the UTC time instead. Depending on your time zone this is more or less far away from what you have entered.

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