Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

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

$
0
0

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


Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>