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

225
Vistas
Javascript Date out by one day (the day before)

So I select two dates and build an array based on this.

For example:

var getDaysArray = function (s, e) { for (var a = [], d = new Date(s); d <= e; d.setDate(d.getDate() + 1)) { a.push(new Date(d)); } return a; };
var daylist = getDaysArray(new Date(datefrom), new Date(dateto));

This returns (daylist) the following:

0: Thu Mar 31 2022 00:00:00 GMT+0800 (Australian Western Standard Time) {}
1: Fri Apr 01 2022 00:00:00 GMT+0800 (Australian Western Standard Time) {}
2: Sat Apr 02 2022 00:00:00 GMT+0800 (Australian Western Standard Time) {}
3: Sun Apr 03 2022 00:00:00 GMT+0800 (Australian Western Standard Time) {}
4: Mon Apr 04 2022 00:00:00 GMT+0800 (Australian Western Standard Time) {}
5: Tue Apr 05 2022 00:00:00 GMT+0800 (Australian Western Standard Time) {}

This is correct. However when I then build these dates:

const thesedays = daylist.map((v) => v.toISOString().slice(0, 10));

This returns (thesedays) the following:

0: "2022-03-30"
1: "2022-03-31"
2: "2022-04-01"
3: "2022-04-02"
4: "2022-04-03"
5: "2022-04-04"

So it is actually using the day before (March 30 instead of 31 and April 4 instead of 5)

It is the const of thesedays that I need to adjust ... just not sure how?

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

0

I can think of two ways ...

const daylist = [
  new Date("2022-01-15T00:00:00"),
  new Date("2022-02-15T00:00:00"),
  new Date("2022-03-15T00:00:00"),
];
const thesedays = daylist.map((v) =>
    `${
        v.getFullYear().toString().padStart(4, "0")
    }-${
        (v.getMonth() + 1).toString().padStart(2, "0")
    }-${
        v.getDate().toString().padStart(2, "0")
    }`
);


console.log(thesedays);

or

const daylist = [
  new Date("2022-01-15T00:00:00"),
  new Date("2022-02-15T00:00:00"),
  new Date("2022-03-15T00:00:00"),
];
const thesedays = daylist.map(
    (v) =>
        new Date(
            Date.parse(
                new Intl.DateTimeFormat("fr-CA", {
                    year: "numeric",
                    month: "2-digit",
                    day: "2-digit",
                }).format(v)
            )
        ).toISOString().split("T")[0]
);

console.log(thesedays)

The first code would be better off with a helper function

const daylist = [
  new Date("2022-01-15T00:00:00"),
  new Date("2022-02-15T00:00:00"),
  new Date("2022-03-15T00:00:00"),
];
const zf = (n, z=2) => n.toString().padStart(z, '0');
const thesedays = daylist.map((v) =>
    `${zf(v.getFullYear(), 4)}-${zf((v.getMonth() + 1))}-${zf(v.getDate())}`
);


console.log(thesedays);

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