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

238
Vistas
Error while pushing Dates to Array in Apps Script

This is the function (I feel I did the same thing a few hours ago and the output was what I was expecting)

function errorFunction() {
  let master = []

  let a = [1,2] //a simple array to loop over

  a.forEach(function(b) {
    let theDate = Date.today() // 8th Sep 2021
    let i = 0
    while (i<2) {
      theDate = Date.parse(theDate).addDays(1)
      Logger.log(theDate) // dates are alternating between 9th and 10th september
      master.push([b,i,theDate])

      i = i+1
    }

  })
  
  Logger.log(master) // all dates are 10th september
}

The code is pushing 9th September and 10th September to the array. But the output of the array has only 10th September.

What I expect :

[
  [1.0, 0.0, Fri Sep 9 00:00:00 GMT+05:30 2021],
  [1.0, 1.0, Fri Sep 10 00:00:00 GMT+05:30 2021],
  [2.0, 0.0, Fri Sep 9 00:00:00 GMT+05:30 2021],
  [2.0, 1.0, Fri Sep 10 00:00:00 GMT+05:30 2021]
]

The Output I get :

[
  [1.0, 0.0, Fri Sep 10 00:00:00 GMT+05:30 2021],
  [1.0, 1.0, Fri Sep 10 00:00:00 GMT+05:30 2021],
  [2.0, 0.0, Fri Sep 10 00:00:00 GMT+05:30 2021],
  [2.0, 1.0, Fri Sep 10 00:00:00 GMT+05:30 2021]
]

I am using the latest version of DateJs library.

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

0

If you pass a Date object to the parse() method of DateJS, it will return the object (link), and the addDate() method also does not create a new object (link). So while you created a new theDate object for every b value, you modified the same object in the while loop, and put references to it into the master array.

The solution is to create a new Date object every time you modify theDate. For example, change this line:

theDate = Date.parse(theDate).addDays(1)

...to this:

theDate = new Date(theDate.valueOf()).addDays(1)

...or this (DateJS defines a clone() method):

theDate = theDate.clone().addDays(1)

This will clone the theDate object before adding 1 day to it.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Not sure why you need that library, this snippet does the job:

const errorFunction = () => {
  const days = [1, 2];
  const i = [0, 1];
  const today = new Date();
  const addDays = (date, numDays = 1) =>
    new Date(new Date().setDate(date.getDate() + numDays));
  return days.reduce((acc, day) => {
    return [...acc, ...i.map(item => [day, item, addDays(today, day)])];
  }, []);
};

console.log(errorFunction());

Let me know if this is what you are expecting.

about 4 years ago · Juan Pablo Isaza Denunciar

0

As @kol pointed out that I was putting references in the Array.

So instead of a reference, I did this :

function errorFunction() {
  let master = []

  let a = [1,2] //a simple array to loop over

  a.forEach(function(b) {
    let theDate = Date.today() // 8th Sep 2021
    let i = 0
    while (i<2) {
      theDate = Date.parse(theDate).addDays(1)
      Logger.log(theDate) // dates are alternating between 9th and 10th september
      master.push([b,i,new Date(theDate)])

      i = i+1
    }

  })
  
  Logger.log(master) // all dates are 10th september
}
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