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

113
Views
Google Apps Script return date between a range of dates in an array

I have an array of dates. I want to return the dates between January 01, 2022 and December 31, 2022.

When I run the cut below, it doesn't return anything. This is one of many different loop variations I've tried to no avail. Please help TIA

var start = new Date(2022, 0, 01); //returns Sat Jan 01 00:00:00 GMT-06:00 2022
var end = new Date(2022, 11, 01); //returns Sat Dec 31 00:00:00 GMT-06:00 2022
var arr = [Fri Dec 03 2021 00:00:00 GMT-0600 (Central Standard Time), 
           Fri Dec 10 2021 00:00:00 GMT-0600 (Central Standard Time), 
           Fri Dec 17 2021 00:00:00 GMT-0600 (Central Standard Time), 
           Fri Dec 24 2021 00:00:00 GMT-0600 (Central Standard Time), 
           Fri Dec 31 2021 00:00:00 GMT-0600 (Central Standard Time), 
           Fri Jan 07 2022 00:00:00 GMT-0600 (Central Standard Time), 
           Fri Jan 14 2022 00:00:00 GMT-0600 (Central Standard Time), 
           Fri Jan 21 2022 00:00:00 GMT-0600 (Central Standard Time), 
           Fri Jan 28 2022 00:00:00 GMT-0600 (Central Standard Time),
           ...](**output** NOT the actual script)
for(var i=0; i<=arr.length; i++){
   if(arr[i] >= start && arr[i] <= end){
      Logger.log(arr[i]);
   }
}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Modification points:

  • From your script, I couldn't understand the value of arr. Because in your script, when you save the script, an error occurs. So I'm worried that your actual script is different from your showing script. If each value of arr is the string type like Fri Dec 03 2021 00:00:00 GMT-0600 (Central Standard Time), please enclose it using the double or single quotes.
    • If my guess of your script is not correct, please provide your actual script.
  • In order to compare the date, I think that this thread will be useful. Ref

When these points are reflected in your script, it becomes as follows.

Modified script:

var start = new Date(2022, 0, 01); //returns Sat Jan 01 00:00:00 GMT-06:00 2022
var end = new Date(2022, 11, 01); //returns Sat Dec 31 00:00:00 GMT-06:00 2022
var arr = [
  "Fri Dec 03 2021 00:00:00 GMT-0600 (Central Standard Time)",
  "Fri Dec 10 2021 00:00:00 GMT-0600 (Central Standard Time)",
  "Fri Dec 17 2021 00:00:00 GMT-0600 (Central Standard Time)",
  "Fri Dec 24 2021 00:00:00 GMT-0600 (Central Standard Time)",
  "Fri Dec 31 2021 00:00:00 GMT-0600 (Central Standard Time)",
  "Fri Jan 07 2022 00:00:00 GMT-0600 (Central Standard Time)",
  "Fri Jan 14 2022 00:00:00 GMT-0600 (Central Standard Time)",
  "Fri Jan 21 2022 00:00:00 GMT-0600 (Central Standard Time)",
  "Fri Jan 28 2022 00:00:00 GMT-0600 (Central Standard Time)",
];
for (var i = 0; i <= arr.length; i++) {
  if (new Date(arr[i]).getTime() >= start.getTime() && new Date(arr[i]).getTime() <= end.getTime()) {
    console.log(arr[i]);
  }
}

References:

  • Related thread
    • Compare two dates with JavaScript
about 4 years ago · Juan Pablo Isaza Report

0

What Tanaike mentioned is correct, it should be a string in your array and missing quotes then you can do

arr.filter(dates => new Date(dates) >= start && new Date(dates) <= end );
about 4 years ago · Juan Pablo Isaza Report

0

I was able to figure it out thanks for your input. The problem was I needed to set arr[i] to new Date(arr[i]). Below is the updated code.

var start = new Date(2022, 0, 01); //returns Sat Jan 01 00:00:00 GMT-06:00 2022
var end = new Date(2022, 11, 01); //returns Sat Dec 31 00:00:00 GMT-06:00 2022
var arr = [Fri Dec 03 2021 00:00:00 GMT-0600 (Central Standard Time), 
           Fri Dec 10 2021 00:00:00 GMT-0600 (Central Standard Time), 
           Fri Dec 17 2021 00:00:00 GMT-0600 (Central Standard Time), 
           Fri Dec 24 2021 00:00:00 GMT-0600 (Central Standard Time), 
           Fri Dec 31 2021 00:00:00 GMT-0600 (Central Standard Time), 
           Fri Jan 07 2022 00:00:00 GMT-0600 (Central Standard Time), 
           Fri Jan 14 2022 00:00:00 GMT-0600 (Central Standard Time), 
           Fri Jan 21 2022 00:00:00 GMT-0600 (Central Standard Time), 
           Fri Jan 28 2022 00:00:00 GMT-0600 (Central Standard Time),
           ...](**output of the array** NOT the actual script)
for(var i=0; i<=arr.length; i++){
   var arrI = new Date(arr[i]);
   if(arrI >= start && arrI <= end){
      Logger.log(arrI);
   }
}
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!