[英]Check if any date/time in array is greater then current local time using Luxon moment js
I am currently getting back 2 sets of times based of some json data.我目前正在根据一些 json 数据返回 2 组时间。 I want to be able to compare the current local time and check if its greater or less then any date in my json scheme.我希望能够比较当前本地时间并检查它是否大于或小于我的 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
You are passing result ( an array ) to timeAgo(date) function which expect iso format date and then ( as date parameter ) trying parse it what cant work.您正在将结果(一个数组)传递给timeAgo(date) function ,它期望 iso 格式日期,然后(作为日期参数)尝试解析它什么不起作用。 Your function work correctly when you pass correct argument.. you can try with proper iso date.当您传递正确的参数时,您的 function 可以正常工作。您可以尝试使用正确的 iso 日期。
timeAgo("2020-09-23T21:45:00.000+0000") // return 255017.36843333332
const diffrences = result.map( res => res.carInstances.map( ins => timeAgo(ins.carTime) )).flat()
console.log("result", diffrences )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.