我想找到距離給定時間最近的下一個時間,我從堆棧溢出中得到了這段代碼,但我無法獲得所需的結果// Current time in millisconst now = +moment('10:07', 'HH:mm').format('x');// List of timesconst times = ["10:00", "10:18", "23:30", "12:00"];// Times in millisecondsconst timesInMillis = times.map(t => +moment(t, "HH:mm").format("x"));function closestTime(arr, time) { return arr.reduce(function(prev, curr) { return Math.abs(curr - time) < Math.abs(prev - time) ? curr : prev; });}const closest = moment(closestTime(timesInMillis, now)).format('HH:mm');// closest is 10:00 but i want the next time 10:18console.log(closest);<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
如何使用矩獲得數組中最近的下一次時間?
料青山看我應如是
2023-11-11 16:14:43