Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

192
Views
find next closest date and time from json response

I am getting a response from my api where start_date is given. I want to extract the id from json whose next date time is closest to current date time.

[
    {
        "expected": {
            "id": 1,
            "end_time": "2021-10-01T06:35:00.659Z",
            "start_time": "2021-10-01T06:35:00.659Z"
        }
    },
    {
        "expected": {
            "id": 2,
            "end_time": "2021-09-30T17:08:29.307Z",
            "start_time": "2021-09-30T17:08:29.307Z"
        }
    },
    {   
        "expected": {
            "id": 3,
            "end_time": "2021-10-01T09:49:18.574Z",
            "start_time": "2021-10-01T09:49:18.574Z"
        }
    },
    {   
        "expected": {
            "id": 4,
            "end_time": "2021-09-30T17:08:29.303Z",
            "start_time": "2021-09-30T15:08:29.303Z"
        }
    },
]

As you can see based on date id:2 and id:4 has the closest start_date from current date, and then based on time id:4 is the closest.

I am not able to figure out how to extract this id. I have been trying for hours but not getting anywhere close to the solution. Please help.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Step 1: Using Array.sort by Date.getTime() value when compare to current datetime.

Step 2: Return first item of sorted array.

const inputArray = [{
    "expected": {
      "id": 1,
      "end_time": "2021-10-01T06:35:00.659Z",
      "start_time": "2021-10-01T06:35:00.659Z"
    }
  },
  {
    "expected": {
      "id": 2,
      "end_time": "2021-09-30T17:08:29.307Z",
      "start_time": "2021-09-30T17:08:29.307Z"
    }
  },
  {
    "expected": {
      "id": 3,
      "end_time": "2021-10-01T09:49:18.574Z",
      "start_time": "2021-10-01T09:49:18.574Z"
    }
  },
  {
    "expected": {
      "id": 4,
      "end_time": "2021-09-30T17:08:29.303Z",
      "start_time": "2021-09-30T15:08:29.303Z"
    }
  },
]

const today = new Date().getTime();
const result = inputArray.sort((a, b) => {
   const diffA = new Date(a.expected.start_time).getTime() - today;
   const diffB = new Date(b.expected.start_time).getTime() - today;
   return diffA - diffB;
})[0];
console.log(result.expected.id);

about 4 years ago · Juan Pablo Isaza Report

0

My Data

const data = [
{
    "expected": {
        "id": 1,
        "end_time": "2021-10-01T06:35:00.659Z",
        "start_time": "2021-10-01T13:35:00.659Z"
    }
},
{
    "expected": {
        "id": 2,
        "end_time": "2021-09-30T17:08:29.307Z",
        "start_time": "2021-09-30T17:08:29.307Z"
    }
},
{
    "expected": {
        "id": 3,
        "end_time": "2021-10-01T09:49:18.574Z",
        "start_time": "2021-10-05T09:49:18.574Z"
    }
},
{
    "expected": {
        "id": 4,
        "end_time": "2021-09-30T17:08:29.303Z",
        "start_time": "2021-09-30T15:08:29.303Z"
    }
}]

Then, I have created a function getNextTime()

getNextTime = () => {
    let nextTimeData = [];
    const sortedData = data.sort((a, b) => new Date(b.expected.start_time) - new Date(a.expected.start_time))
    sortedData.forEach((element) => {
        if (new Date() <= new Date(element.expected.start_time)) {
            nextTimeData.push(element)
        }
    })
    return nextTimeData
}

That function gives you all the next date-time, you can use last index of nextTimeData as closet date-time like :

let data = this.getNextTime();
console.log('closet date time id ::', data[data.length - 1].expected.id);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!