These are the dates i have in this format coming from a Mysql database, where i need to dynamically get all the fields in this array that fit in the last 24 hour window of now. the issue is that the js date comes in a different format, so i am having a hard time comparing them in a clean and efficient way.
This is the date array :
let userTransactions = [
{
id: 1,
created_at: "2022-08-18 12:15:12",
updated_at: "2022-08-19 12:15:12",
},
{
id: 2,
created_at: "2022-08-19 10:15:12",
updated_at: "2022-08-19 12:15:12",
},
{
id: 3,
created_at: "2022-08-19 16:15:12",
updated_at: "2022-08-19 12:15:12",
},
{
id: 4,
created_at: "2022-08-19 05:15:12",
updated_at: "2022-08-19 12:15:12",
},
{
id: 5,
created_at: "2022-08-19 11:15:12",
updated_at: "2022-08-19 12:15:12",
},
{
id: 6,
created_at: "2022-08-19 08:15:12",
updated_at: "2022-08-19 12:15:12",
},
];
this is the function :
const filterTo24Hours = (userTransactions) => {
let dayTransactions = userTransactions.filter((item) => {
let date = new Date();
// this if statement is written to show what i want to have achieved, this is not working
if (date >= item.created_at && date - 1 <= item.created_at) {
//Do something here
console.log(item.created_at);
}
});
return dayTransactions;
}