Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

105
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda