Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

161
Vistas
Typescript get two slots from array time slots close to current time

Filter the two range close to current time. ex: current time is 10.17 AM(IST), should return: 10.15 & 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);

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Assume timeArry is sorted in ascending order, you can use findIndex to find the first value that's larger than current time, and index - 1 would be the value that's smaller but next close to current time (if it exists):

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

about 4 years ago · Juan Pablo Isaza Denunciar

0

please check out this code, and see if you find something useful

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

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda