Home > Back-end >  find next closest date and time from json response
find next closest date and time from json response

Time:10-04

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.

CodePudding user response:

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);

CodePudding user response:

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);
  • Related