Javascript 在日期之间过滤以获取特定日期范围内的记录

Javascript filtering between dates to get records in specific date range

我正在调用 API 调用 return 大量数据。我想从该数据中过滤掉并仅获取截止日期大于或等于今天且小于或等于从现在起 30 天的记录。

但是我的过滤器return输入了一组不正确的记录。

addDays = (date, days) => {
        var result = new Date(date);
        result.setDate(result.getDate() + days);
        return result;
    }

let today = new Intl.DateTimeFormat('en-US').format(new Date()); //returns "07/31/2020" as expected
let add30 = new Intl.DateTimeFormat('en-US').format(this.addDays(today, 30)); //returns "08/30/2020 as expected

let dueInThirtyDays = allActivities.filter(activity => new Intl.DateTimeFormat('en-US').format(new Date(activity.dueDate)) >= today && new Intl.DateTimeFormat('en-US').format(new Date(activity.dueDate)) <= add30 && new Intl.DateTimeFormat('en-US').format(new Date(activity.closedDate) === null));


//where format of activity.dueDate = "2020-01-14" which is whay its wrapped in the DateTimeFormat method

我过滤的对象数组示例:

{
typeCode: "BCA "
categoryCode: "PN "
activityDescription: "PNACT03 TIER2 PRIMARY"
effectiveDate: "2010-10-14"
statusDate(pin): null
closedDate(pin): "2012-06-30"
dueDate(pin): "2011-01-14"
}

我注意到格式字符串 return 的日期比给定日期早一天,但我得到的结果是今天之前的几个月和从今天起 30 天之后。 它也是 return 结果,其中 closedDate 不为 null

请尝试以下解决方案

  const data = [
    {
      typeCode: "BCA ",
      categoryCode: "PN ",
      activityDescription: "PNACT03 TIER2 PRIMARY",
      effectiveDate: "2010-10-14",
      'statusDate(pin)': null,
      'closedDate(pin)': "2012-06-30",
      'dueDate(pin)': "2011-01-14",
        },
    {
      typeCode: "VGA ",
      categoryCode: "PN ",
      activityDescription: "PNACT03 TIER2 PRIMARY",
      effectiveDate: "2010-10-14",
      'statusDate(pin)': null,
      'closedDate(pin)': "2012-06-30",
      'dueDate(pin)': "2011-01-14",
        },
    {
      typeCode: "ABC ",
      categoryCode: "PN ",
      activityDescription: "PNACT03 TIER2 PRIMARY",
      effectiveDate: "2010-10-14",
      'statusDate(pin)': null,
      'closedDate(pin)': "2012-06-30",
      'dueDate(pin)': "2020-08-14",
        }
  ]
  const from = Date.now()
  const until = new Date().setDate(new Date().getDate() + 30)
  
  const output = data.filter(entry => {
    const time = new Date(entry['dueDate(pin)']).getTime()
    
    return time >= from && time <= until
  })
  
  console.log(output)
  

您的问题在于您用于比较的日期格式。

让我们以这两个日期为例:

  1. “10/31/2020”
  2. “11/30/2019”

人类可以看到 2020 年的日期不小于 2019 年的日期,但是对于 if-comparison 这个:

if ("10/31/2020" <= "11/30/2019")

等同于写法:

if ("10312020" <= "11302019")

您可以在浏览器控制台中键入

来尝试此操作
console.log("10/31/2020" <= "11/30/2019");

您可能应该使用 unix 时间戳进行日期比较,或者如果您想与字符串进行比较,则使用 y/m/d 格式。

好的 - 我最终使用了@Mario 和@Christoph Kerns 答案的组合。 我的最终解决方案如下所示:

const fromUnix = new Date().getTime() / 1000;
const untilUnix = new Date().setDate(new Date().getDate() + 30)/ 1000;
let dueInThirtyDays = this.props.activities.allActivities.filter(activity =>(new Date(activity.dueDate).getTime() / 1000) >= fromUnix &&  (new Date(activity.dueDate).getTime() / 1000) <= untilUnix);

我最终决定将@Christooph 标记为正确答案,因为 (1) 他提供了一个完整的解释,并且 (2) 我认为转换为 UNIX 时间才是真正的诀窍。 @Mario 确实提供了一些有助于得出正确答案的语义。谢谢。