gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 01:35:11 25 4
gpt4 key购买 nike

我正在进行一个返回大量数据的 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"
}

我注意到格式字符串返回的日期比给定日期早一天,但我得到的结果是今天之前几个月和从今天起 30 天之后。它还返回 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)

关于日期之间的Javascript过滤以获取特定日期范围内的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63197326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com