Filtre los dos rangos cerca de la hora actual. ej.: la hora actual es 10:17 a. m. (IST), debe regresar: 10:15 y 10:30
let timeArry = ["08:00", "08:15", "08:30", "08:45", "09:00", "09:15", "09:30", "09:45", "10:00", "10:15", "10:30", "10:45", "11:00", "11:15", "11:30", "11:45", "12:00", "12:15", "12:30", "12:45", "13:00", "13:15", "13:30", "13:45", "14:00", "14:15", "14:30", "14:45", "15:00", "15:15", "15:30", "15:45", "16:00", "16:15", "16:30", "16:45", "17:00", "17:15", "17:30", "17:45", "18:00", "18:15", "18:30", "18:45", "19:00", "19:15", "19:30", "19:45", "20:00", "20:15", "20:30", "20:45", "21:00", "21:15", "21:30"]; let currentTime = new Date().getHours()+':'+new Date().getMinutes(); let result = timeArry.filter(x => x.includes(currentTime)); console.log(result);Suponga que timeArry está ordenado en orden ascendente, puede usar findIndex para encontrar el primer valor que es más grande que la hora actual, y index - 1 sería el valor que es más pequeño pero próximo a la hora actual (si existe):
let timeArry = ["08:00", "08:15", "08:30", "08:45", "09:00", "09:15", "09:30", "09:45", "10:00", "10:15", "10:30", "10:45", "11:00", "11:15", "11:30", "11:45", "12:00", "12:15", "12:30", "12:45", "13:00", "13:15", "13:30", "13:45", "14:00", "14:15", "14:30", "14:45", "15:00", "15:15", "15:30", "15:45", "16:00", "16:15", "16:30", "16:45", "17:00", "17:15", "17:30", "17:45", "18:00", "18:15", "18:30", "18:45", "19:00", "19:15", "19:30", "19:45", "20:00", "20:15", "20:30", "20:45", "21:00", "21:15", "21:30"]; let currentTime = "08:17"; let index = timeArry.findIndex(x => x.localeCompare(currentTime) >= 0); if (index !== -1) { console.log("Value after current time: ", timeArry[index]); if (index !== 0) { console.log("Value before current time: ", timeArry[index-1]); } }por favor revise este código, y vea si encuentra algo útil
let timeArray = ["08:00", "08:15", "08:30", "08:45", "09:00", "09:15", "09:30", "09:45", "10:00", "10:15", "10:30", "10:45", "11:00", "11:15", "11:30", "11:45", "12:00", "12:15", "12:30", "12:45", "13:00", "13:15", "13:30", "13:45", "14:00", "14:15", "14:30", "14:45", "15:00", "15:15", "15:30", "15:45", "16:00", "16:15", "16:30", "16:45", "17:00", "17:15", "17:30", "17:45", "18:00", "18:15", "18:30", "18:45", "19:00", "19:15", "19:30", "19:45", "20:00", "20:15", "20:30", "20:45", "21:00", "21:15", "21:30"]; let currentTime = new Date().getHours()+':'+new Date().getMinutes(); // build time pairs const timePairs = timeArray.reduce((acc, cur, index) => { if(index === 0) { return acc; } const pair = [timeArray[index-1], timeArray[index]]; return [...acc, pair]; }, []); console.log(timePairs); // utility function to parse time const parseSlot = (slot) => { const tokens = slot.split(":").map(t => parseInt(t)); const hourInMinutes = tokens[0] * 60; return hourInMinutes + tokens[1]; } // comparison function const inSlots = (current, slot1, slot2) => parseSlot(current) >= parseSlot(slot1) && parseSlot(current) <= parseSlot(slot2); const result = timePairs.find(([slot1, slot2]) => inSlots(currentTime, slot1, slot2)); console.log(result);