I am making an API call that returns a large set of data. From that data I want to filter out and get only the records with a due date greater then or equal to today and less then or equal to 30 days from now.
But my filter is returning an incorrect set of records.
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
EXAMPLE of the array of Objects I am filtering through:
{
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"
}
I am noticing that the format string return a date that is one day before the date given but I'm getting results back that are months ad years before today and well after 30 days from today.
Its also return results where closedDate is not null