I have objects with different dates. How I can filter out the past dates and present date. I have dates with from past and future, I want only to past dates and the current dates from the objects. I try filtering but I'm not sure what wrong I'm doing My CODE :
const arr = [{
"id": "e18dae03-99c7-4ffa-be2f-a595fc7fad15",
"event_date": "2021-09-30T01:00:00.000Z",
"alert_name": null,
"alert_level": null,
"alert_action": null,
"alert_details": null,
},
{
"id": "e18dae03-99c7-4ffa-be2f-a595fc7fad15",
"event_date": "2021-10-30T01:00:00.000Z",
"alert_name": null,
"alert_level": null,
"alert_action": null,
"alert_details": null,
}
{
"id": "765a508f-fb47-4de4-bf69-19f0db38ccfc",
"event_date": "2021-09-17T22:03:00.000Z",
"alert_name": null,
"alert_level": null,
"alert_action": null,
"alert_details": null,
},
{
"id": "bc07a114-43f4-49a1-bb6c-fec801aee749",
"longitude": null,
"event_date": "2021-11-19T07:51:00.000Z",
"alert_name": null,
"alert_level": null,
"alert_action": null,
"alert_details": null,
}
]
const filterByExpiration = () => arr.filter(({ event_date }) => event_date <= new Date());
CodePudding user response:
event_date
is a string. You should convert it to a Date
object first.
const filterByExpiration = () => arr.filter(({ event_date }) => new Date(event_date) <= new Date());
Or compare it to an ISO date string
const filterByExpiration = () => arr.filter(({ event_date }) => event_date <= new Date().toISOString());
CodePudding user response:
Strings can not be compared with Dates. You need to parse event_date
to a Date
using new Date(event_date)
. The proper comparison is
new Date(event_date) <= new Date()
const arr = [{
"id": "e18dae03-99c7-4ffa-be2f-a595fc7fad15",
"event_date": "2021-09-30T01:00:00.000Z",
"alert_name": null,
"alert_level": null,
"alert_action": null,
"alert_details": null,
},
{
"id": "e18dae03-99c7-4ffa-be2f-a595fc7fad15",
"event_date": "2021-10-30T01:00:00.000Z",
"alert_name": null,
"alert_level": null,
"alert_action": null,
"alert_details": null,
},
{
"id": "765a508f-fb47-4de4-bf69-19f0db38ccfc",
"event_date": "2021-09-17T22:03:00.000Z",
"alert_name": null,
"alert_level": null,
"alert_action": null,
"alert_details": null,
},
{
"id": "bc07a114-43f4-49a1-bb6c-fec801aee749",
"longitude": null,
"event_date": "2021-11-19T07:51:00.000Z",
"alert_name": null,
"alert_level": null,
"alert_action": null,
"alert_details": null,
}
]
const filterByExpiration = () => arr.filter(({ event_date }) => new Date(event_date) <= new Date());
console.log(filterByExpiration());
CodePudding user response:
Your event_date
is a string. That is why the comparison is giving results you do not expect. You need to convert the string into a Date object.
const arr = [{
"id": "e18dae03-99c7-4ffa-be2f-a595fc7fad15",
"event_date": "2021-09-30T01:00:00.000Z",
"alert_name": null,
"alert_level": null,
"alert_action": null,
"alert_details": null,
},
{
"id": "e18dae03-99c7-4ffa-be2f-a595fc7fad15",
"event_date": "2021-10-30T01:00:00.000Z",
"alert_name": null,
"alert_level": null,
"alert_action": null,
"alert_details": null,
},
{
"id": "765a508f-fb47-4de4-bf69-19f0db38ccfc",
"event_date": "2021-09-17T22:03:00.000Z",
"alert_name": null,
"alert_level": null,
"alert_action": null,
"alert_details": null,
},
{
"id": "bc07a114-43f4-49a1-bb6c-fec801aee749",
"longitude": null,
"event_date": "2021-11-19T07:51:00.000Z",
"alert_name": null,
"alert_level": null,
"alert_action": null,
"alert_details": null,
}
]
const filterByExpiration = () => arr.filter(({ event_date }) => {
let x = new Date(event_date);
return (x <= new Date());
});
console.log(filterByExpiration());
CodePudding user response:
You have to convert the node event_date
in each node of the arr
to date object to make comparison. As of now its a string.
Also you have to call filterByExpiration
function. You have just defined that, it has not been called.
Working Fiddle
const arr = [{"id":"e18dae03-99c7-4ffa-be2f-a595fc7fad15","event_date":"2021-09-30T01:00:00.000Z","alert_name":null,"alert_level":null,"alert_action":null,"alert_details":null},{"id":"e18dae03-99c7-4ffa-be2f-a595fc7fad15","event_date":"2021-10-30T01:00:00.000Z","alert_name":null,"alert_level":null,"alert_action":null,"alert_details":null},{"id":"765a508f-fb47-4de4-bf69-19f0db38ccfc","event_date":"2021-09-17T22:03:00.000Z","alert_name":null,"alert_level":null,"alert_action":null,"alert_details":null},{"id":"bc07a114-43f4-49a1-bb6c-fec801aee749","longitude":null,"event_date":"2021-11-19T07:51:00.000Z","alert_name":null,"alert_level":null,"alert_action":null,"alert_details":null}];
const filterByExpiration = () => arr.filter(({ event_date }) => new Date(event_date) <= new Date());
console.log(filterByExpiration())