我目前正在根据一些 json 数据返回 2 组时间。我希望能够比较当前本地时间并检查它是否大于或小于我的 json 方案中的任何日期。
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