Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

477
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!