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

106
Visualizações
moment avoid time zone after calculation

I need to make calcul with date in moment but i just want to count days, i don't care about hours, timezone or whatever.

I store date in date without timezone, but when i made calculations with moment, I always have problem with hours changing. My computer is in France and we change hour 2 time per years.

I found some way with parseZone() and startOf('day'), but my code become very messy and i'm looking for the best way to do what i want.

For example, this code : http://jsfiddle.net/zn2pcg65/2/

var a = moment('2022-01-05T00:00:00.000Z');
console.log(a.toDate().toISOString());
var b = a.add(3,'M');
console.log(b.toDate().toISOString());

give me :

"2022-01-05T00:00:00.000Z"
"2022-04-04T23:00:00.000Z"

I wanted "2022-04-05".

What is the best way to do calculations with date without having this kind of side effect ?

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

0

The problem is that you are using ISO format, so what is ISO format

ISO: The International Organization for Standardization (ISO) date format is a standard way to express a numeric calendar date that eliminates ambiguity. For example, North Americans usually write the month before the date.

for more: https://www.techtarget.com/whatis/definition/ISO-date-format

To solve your problems you need to parse your date to UTC time.

What its etc:

Coordinated Universal Time (UTC) is the basis for civil time today. This 24-hour time standard is kept using highly precise atomic clocks combined with the Earth's rotation.

for more: https://www.timeanddate.com/time/aboututc.html

Here the solution you need:

var a = moment('2022-01-05T00:00:00.000Z').utc();
console.log('a: ',a.toDate().toISOString());
var b = a.add(3,'M');
console.log('b', b.toDate().toISOString());

//more espesific format: 
//a.format('MM-DD-YYYY') output 01-05-2022

//b.format('MM-DD-YYYY') output 04-05-2022
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