简体   繁体   English

使用 Luxon moment js 检查数组中的任何日期/时间是否大于当前本地时间

[英]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.

相关问题 如何使用angularjs检查所选日期和所选时间小于当前日期或大于当前日期和时间? - How to check selected date and selected time is less than current or greater than current date and time using angularjs? 使用 Moment.js 将 UTC 日期和时间转换为本地日期和时间 - Convert UTC Date and Time to local Date and Time using Moment.js 如何使用 Luxon 格式化时间(不是日期)? - How to format a time (not a date) using Luxon? 通过使用moment.Js通过提供用户定义的时间值来创建当前日期和时间 - using moment.Js to create current date and time by giving user defined values for time 使用 moment.js 检查本地时间是否在 2 个其他时区时间之间 - Using moment.js to check if local time is between 2 other time zones times 我如何在 javascript 中使用 moment.js/Date 将 EST 时间数据转换为本地时间 - how I convert EST time data to local time using moment.js / Date in javascript js:如何将时间转换为日期(使用时刻) - js : how to convert time to date (using moment) 使用 Moment 和 React JS 格式化日期和时间 - Formatting date and time using Moment with React JS 可以将日期转换为当地时间吗? - Moment Converting Date to Local Time? 使用moment.js处理本地化日期以将其转换为本地日期/时间 - Processing localised date to convert it to local date/time using moment.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM