Home > Software engineering >  Typescript get two slots from array time slots close to current time
Typescript get two slots from array time slots close to current time

Time:10-17

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);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

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]);
  }
}
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

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);
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related