Google Apps 脚本 return 数组中日期范围之间的日期

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

我有一组日期。我想要 return 2022 年 1 月 1 日和 2022 年 12 月 31 日之间的日期。

当我 运行 下面的剪辑时,它 return 什么都没有。这是我尝试过但无济于事的许多不同循环变体之一。请帮助 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]);
   }
}

修改点:

  • 根据您的脚本,我无法理解 arr 的值。因为在你的脚本中,当你保存脚本的时候,出现了一个错误。所以我担心您的实际脚本与您的显示脚本不同。如果arr的每个值都是Fri Dec 03 2021 00:00:00 GMT-0600 (Central Standard Time)这样的字符串类型,请用双引号或单引号括起来。
    • 如果我对您的脚本的猜测不正确,请提供您的实际脚本。
  • 为了比较日期,我认为这个帖子会有用。 Ref

当这些点体现在你的脚本中,就变成了下面这样。

修改后的脚本:

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]);
  }
}

参考文献:

  • 相关话题
    • Compare two dates with JavaScript

Tanaike提到的是正确的,它应该是你的数组中的一个字符串并且缺少引号然后你可以这样做

arr.filter(dates => new Date(dates) >= start && new Date(dates) <= end );

我已经弄明白了,感谢您的输入。问题是我需要将 arr[i] 设置为 new Date(arr[i])。下面是更新后的代码。

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);
   }
}