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

484
Visualizações
How to subtract 5 days from a defined date - Google App Script

I'm trying to write a script to subtract 5 days from a defined date but seems not working, here's my code:

var End_Day = sheet.getRange(i + 2, 20).getValue();
Logger.log(End_Day);
var End_day_2 = new Date();
End_day_2.setDate(End_Day.getDate()-5);
Logger.log(End_day_2);

and the result is not just - 5 days:

11:18:47 AM Info    Sat Jun 04 00:00:00 GMT+08:00 2022
11:18:47 AM Info    Fri Apr 29 11:18:47 GMT+08:00 2022

I am quite confused why the date move from Jun to Apr.

Thanks for having a look

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

0

Try:

var End_Day = sheet.getRange(i + 2, 20).getValue();
var End_day_2 = new Date(End_Day.getTime() - (5 * (1000 * 60 * 60 * 24)))
Logger.log(End_Day);
Logger.log(End_day_2);

Function:

const endDay = sheet.getRange(i + 2, 20).getValue()
const endDay2 = DateFromDaysAgo(endDay, 5)

...

function DateFromDaysAgo(startDate, number) {

  if (typeof startDate === `string`) { startDate = new Date(startDate) }

  return new Date(startDate.getTime() - (number * (1000 * 60 * 60 * 24)))

}
about 4 years ago · Juan Pablo Isaza Relatório

0

If what's coming from the sheet is a string, you will have to convert the date string into a date object.

The other thing is you have to work in milliseconds as @vanowm says:

606024*5 = 432000 * 1000 = 432000000

so skipping the sheet entirely:

x = new Date
> Fri May 27 2022 11:24:01 GMT-0400 (Eastern Daylight Time)

y = new Date(x - 432000000)
> Sun May 22 2022 11:24:01 GMT-0400 (Eastern Daylight Time)
about 4 years ago · Juan Pablo Isaza Relatório

0

You should learn more about Date.prototype.setDate().It only changes the day of the month of a given Date instance.

As the code you posted, the day of the month of End_Day is 4, End_day_2.setDate(4 - 5) equals to End_day_2.setDate(-1) and the month of End_day_2 is April according to the console result, because there're 30 days in April, setDate(-1) means setDate(29), so you got Apr 29 at the end. That's how it goes.

One right way to do is substracting 5 days worth of milliseconds.

function addDays(date, days){
  const DAY_IN_MILLISECONDS = 24 * 60 * 60000;
  return new Date(date.getTime() + days * DAY_IN_MILLISECONDS);
}

console.log(addDays(new Date(), -5).toString()); // 5 days ago

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