Verify if any date/time within an array is later than the current local time by utilizing Luxon and Moment.js

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

Answer №1

Your function is expecting an ISO format date when you pass an array called result to the timeAgo(date) function. However, it fails to parse the date as a parameter. Make sure to input a valid ISO date for the function to work correctly.

timeAgo("2020-09-23T21:45:00.000+0000") // returns 255017.36843333332
const differences = result.map(res => res.carInstances.map(ins => timeAgo(ins.carTime))).flat()

console.log("result", differences)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Multer failing to generate file during request process

My current setup involves a router and multer middleware, but I'm facing an issue where the file requested is not being created. As a result, req.file always remains undefined. const multer = require('multer'); let storage = multe ...

Creating a fixed sidebar that remains visible while scrolling in Next.js

Currently, I am faced with the challenge of implementing two components - a feed and a sidebar. The sidebar contains more content than it can display at once, so I want it to be able to overflow. My goal is to have the sidebar scroll along with the content ...

Scrolling through four limited list items automatically

Hey there! I am currently working on a design project. You can check it out here. I'm trying to replicate a section from another site, which you can see here. <div class="latest-winners-container"> <h3 class="header">Latest Winners< ...

Determine the time left and check the speed of file uploads using ajax with jquery or javascript

Here is a snippet of code using jQuery.ajax to handle file uploads with progress tracking functionality. The function updates the DOM elements with information about bytes uploaded, total bytes, and percentage completed. However, I am looking for addition ...

When utilizing fs.writefile with an object, the output will be [object object]

I need assistance with transferring form data to a json file for the first time. After running my code, the json file only shows [object Object] instead of the expected email, username, and password values. Below is my server-side code (expressjs): app.p ...

Different ways to enhance max-http-header-size in Vue application

After being redirected from another application, I am unable to open the page and receive an error in the console: Failed to load resource: the server responded with a status of 431 (Request Header Fields Too Large). I came across information about max-h ...

Having trouble updating the dropdown value in a React child component

There are two main components in my code: App component: class App extends Component { constructor(props) { super(props); this.state = { selectedScale: "WholeTone" }; this.handleScaleSelect = this.handleScaleSelect.bind(this); } ...

Proper method for refreshing React context

Currently, I am implementing Gatsby along with a layout plugin to maintain the persistence of my navbar while the content on the page below it changes. My main objective is to have animations run smoothly during page transitions without the navbar reloadin ...

Utilizing Javascript to collapse the initial DIV element discovered with a specific classname

Seeking assistance with this particular Javascript code. The objective is to collapse the next element. Unfortunately, it is not functioning as expected due to an error in closing the first DIV in each block. The script should aim to collapse the initial ...

pagination functionality incorporated into element ui tables

Issue with Element UI: when a checkbox is selected and the page is changed, the selected rows' checkboxes are removed. I need to retain the selection items while paging so that users can select items from multiple pages without losing the selections f ...

Discovering the Occurrences of each Object in a List using ReactJS

Struggling with arrays of objects in React.js. My list of objects looks like this : const list = [ { name: name1, age: age1, address: addresse1 }, { name: name2, age: age2, address: addresse2 }, { name: name1, age: age1, address: addresse4 }, { name ...

Infinite error loop during start-up in AngularJS application due to digest cycle overflow

Just starting out with Angular and working on a new project using node, jade, and angular. I'm having trouble implementing a catch-all server route. Every time the index page loads, it gets stuck in a loop and crashes the app. I've tried several ...

What steps are involved in creating an xlsx file using Express and Exceljs, and then sending it to the client?

I am currently working on separating controllers and services in my Express app. One of the services I have generates an XLSX file using ExcelJS. I'm looking for a way to reuse this service without passing the response object to it. Is there a method ...

What is causing this code to break when using ajax's `data` parameter?

I'm relatively new to JavaScript, and I've been working on some code that seems to be properly formatted. However, whenever I add the data elements, it breaks the code. I've checked the jQuery documentation and as far as I can tell, I'm ...

Ways to personalize the interface following the selection of an item from a dropdown menu

Currently, I am using Vue.js and Typescript for my project work. I have a requirement to customize the interface by making adjustments based on the selected item from a drop-down list. <b-form-select id="input-topic" ...

Tips for attaching a React hook to a DOM element generated using a jQuery function

I have a project written in jQuery that I am looking to integrate into my React project. My goal is to send a query to my graphql server when a button is clicked. Within my jQuery code, there is a function that creates multiple elements as shown below: c ...

React - group several elements within a wrapping container

I am dealing with an array of words that make up sentences: let words = [ { "start_time": "2.54", "end_time": "3.28", "alternatives": [ { "confidence": "1.0", ...

Using Jquery and Ajax to pass an extra PHP variable to a server-side script

I am working with a dropdown select box where the selected option is sent to a server-side script using Ajax. <select id="main_select"> <option selected="selected" value="50">50</option> <option ...

Utilizing the Django object array in AngularJS – a comprehensive guide

I have a Django variable structured like this: [<Topic object>, <Topic object>] When passing it to Angular using ng-init, I wrote the following: <div class="profile-navigation" ng-init="ProfileTopics={{ProfileTopics|safe}} "> However, ...

Oops! Looks like something went wrong. The command to install the debug app failed because the emulator could not be launched. This could be due to the fact that no emulators were found

I'm in need of assistance to resolve these issues Encountered an error while trying to install the app. Please ensure that your Android development environment is properly configured: https://reactnative.dev/docs/environment-setup. Error: Command fai ...