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

127
Views
find closest future date and time from today's from in json response

I am getting a response from my api where start_time is given. I want to extract the id from json whose next date time is closest to current date time which does not include dates from past.

An example of my json response:

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

Here what I have done untill now is to find the closest date time from today's date time:

const today = new Date().getTime();
const result = response.sort((a: any, b: any) => {
    const diffA =
        new Date(a.expected.shift_start_time).getTime() - today;
    const diffB =
        new Date(b.expected.shift_start_time).getTime() - today;
    return diffA - diffB;
})[0];

using this I am getting the id of closest date time from today. But the result sometimes also include a start_time from past date if that is closest to todays date.

I want to get id which does not include start_time from past dates. I am unable to get the logic how it can be done

Another approach which I did:

var today = moment();
for (let index = 0; index < response.length; index++) {
    const element = response[index].start_time;
    var dateDiff = today.diff(element, 'days') * -1;
    if (dateDiff >= 0) {
        console.log('>>d>>', dateDiff);
    }
}

console:

>>d>> -0
>>d>> 1
>>d>> -0

From here how can I return the required response?? please help

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

0

Maybe don't sort the list at all. Since you only need one dateTime which is closest with current Date, sorting the entire 'list' doesn't appear to be the optimal way to have your answer.You can use the following approach,

  • Save current DateTimeStamp to a local var
  • Start a loop through your list
  • Perform a check for your current item if the date is greater than local timeStamp you stored.
  • For first element, store the element as your current answer and for every other element compare the diff with the saved timeStamp and modify your answer accordingly.
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!