I am building a website using create-react-app and I need to get the upcoming time from the list as compared to the current time. my time list is
let times = [
{
time: "05:33",
},
{
time: "12:20",
},
{
time: "15:23",
},
{
time: "17:46",
},
{
time: "19:08",
},
];
I have tried many solutions but all in vain, one solution that is also unsuccessful is
const compareTime = moment().format("hh:mm");
time7.map((item, i) => {
console.log(
Date.parse(
`01/01/2011 ${format(parse(item.time, "HH:mm", new Date()), "hh:mm")}45`
) > Date.parse(`01/01/2011 ${compareTime}:10`)
);
});
I am super stuck in this and trying to solve this problem for almost a week I need a proper solution to get the upcoming time from the list any kind of help will be appreciated.
let currentTime = new Date();
currentTime = currentTime.getHours().toString().padStart(2, '0')
+ currentTime.getMinutes().toString().padStart(2, '0');
let futureTimes = times.filter((obj) => {
return obj.time > currentTime;
})
.sort((a, b) => {
return a.time < b.time ? -1 : a.time > b.time;
});
let nextTime = futureTimes.length ? future times[0] : null;
I probably need to double check it and tweak, but coding on my mobile is a little tough.
Simpler solution:
let currentTime = new Date();
currentTime = currentTime.getHours().toString().padStart(2, '0')
+ currentTime.getMinutes().toString().padStart(2, '0');
let nextTime = times
.sort((a, b) => {
return a.time < b.time ? -1 : a.time > b.time;
})
.find((obj) => {
return obj.time > currentTime;
});