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

133
Vistas
Javascript SetMonth Issue

I am doing unit tests.

  it("Month calculate", () => {
    const baseDate = new Date("2015-02-15T12:00:00.000Z");
    baseDate.setMonth(baseDate.getMonth() + 1)
    expect(baseDate.toISOString()).toBe("2015-03-15T12:00:00.000Z")
  })

On my local node process I get this error

Expected: "2015-03-15T12:00:00.000Z"
Received: "2015-03-15T13:00:00.000Z"

On docker node process it works.

I believe it is related to the DST, but I don't understand how...

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You're right that DST is coming into play, because you're using the local time setMonth/getMonth methods, so DST comes into it (adding the month, the Date object takes DST into account to avoid changing the time-of-day; but when you look at the result in UTC, you see the time is off by the DST offset, since it was local time math that applied to the increment). To work in UTC like the rest of your code, use setUTCMonth/getUTCMonth instead.

it("Month calculate", () => {
    const baseDate = new Date("2015-02-15T12:00:00.000Z");
    baseDate.setUTCMonth(baseDate.getUTCMonth() + 1)
    expect(baseDate.toISOString()).toBe("2015-03-15T12:00:00.000Z")
})

Likely your docker node is working UTC, or at least in a time zone where DST doesn't change during that month (it doesn't here in the UK, for instance; we don't change until later in March). But in your local timezone, apparently DST occurs during that month, so you see the issue locally but not on the docker node.

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