JavaScript日期筛选以获取特定日期范围内的记录。

3

我正在调用一个返回大量数据的API。我想从这些数据中过滤出并获取只有截止日期在今天及之后且不晚于30天之间的记录。

但我的筛选器返回了一组错误的记录。

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"
}

我注意到格式字符串返回的日期比给定日期早一天,但是我得到的结果比今天早几个月甚至几年,也有返回 closedDate 不为 null 的结果。

3个回答

4

请尝试以下解决方案。

  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
如果你能为OP添加一些解释,说明你是如何做到的以及为什么这样做(就像这样),那么你的回答将更有价值! - muka.gergely
这个例子对我来说返回了0个结果,但实际上应该返回25条记录。 - reactFullStackDeveloper
你是对的,Muka。基本上我建议将你的日期字符串转换为时间戳,然后像数字一样进行比较。 - Mario
你可以分享一些样本数据和期望的输出结果吗?谢谢。 - Mario

1

你的问题在于使用的日期格式与比较不符。

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

  1. "10/31/2020"
  2. "11/30/2019"

人类可以看出2020年的日期不小于2019年的日期,但是对于if比较而言,以下内容是这样的:

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

与编写以下内容相同:

if ("10312020" <= "11302019")

你可以在浏览器控制台中输入以下内容尝试:
console.log("10/31/2020" <= "11/30/2019");

你应该使用Unix时间戳进行日期比较,或者如果要使用字符串进行比较,则应使用y/m/d格式。

1

好的 - 我最终采用了 @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也提供了一些有助于得出正确答案的语义。谢谢。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接