我正在调用一个返回大量数据的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 的结果。