I am currently receiving 2 sets of times based on some JSON data. I aim to compare the current local time and determine if it is greater or less than any date in my JSON structure.
const result = [
{
"carId": "13122656-169f-45fa-be47-26c9d23dcb7b",
"carInstances": [
{
"carInstanceId": "47209558-f9e1-4f81-a600-5da6ce238a6e",
"carTime": "2020-09-23T21:45:00.000+0000",
"state": "COMPLETE",
}
],
"isPaused": false,
},
{
"carId": "13122656-169f-45fa-be47-26c9d23dcb7b",
"carInstances": [
{
"carInstanceId": "47209558-f9e1-4f81-a600-5da6ce238a6e",
"carTime": "2020-09-23T21:45:00.000+0000",
"state": "COMPLETE",
}
],
"isPaused": false,
},
];
const timeAgo = (date) => {
const now = DateTime.local();
const past = DateTime.fromISO(date);
const diff = past.diff(now, 'minutes');
return Math.floor(diff.minutes);
}
const listCarTime = (res) => {
return res.reduce((acc, car) => {
acc.push(...car.carInstances.map((instance) => instance.carTime));
return acc;
}, []);
}
console.log('result', timeAgo(results)); // Returns NaN