Tengo una función returnTimesInBetween. Toma dos valores (inicio, final) como parámetros y devuelve la matriz de tiempo dividiéndola en los 30 minutos del marco de tiempo. el problema es que cuando la hora de inicio finaliza con 30 minutos como "12:30,11:30,15:30", no toma ese valor en el marco de tiempo y cuando la hora de inicio es como 11:00, 12:00, etc., funciona bien.
//DIVIDING THE TIME FRAME IN 30 MINUTES OF THE TIME FRAME. function returnTimesInBetween(start, end) { var timesInBetween = ([]); console.log(timesInBetween); var startH = parseInt(start.split(":")[0]); var startM = parseInt(start.split(":")[1]); var endH = parseInt(end.split(":")[0]); var endM = parseInt(end.split(":")[1]); if (startM == 30) startH++; for (var i = startH ; i < endH; i++) { timesInBetween.push(i < 10 ? "0" + i + ":00" : i + ":00") timesInBetween.push(i < 10 ? "0" + i + ":30" : i + ":30"); } if (endM == 00) timesInBetween.push(endH + ":30"); if (endM == 30) timesInBetween.push(endH + ":30") } console.log('time range:-') console.log(returnTimesInBetween("11:30", "15:30")); console.log(returnTimesInBetween("18:00", "21:30"));salida de la consola son
[]0: "12:00" 1: "12:30" 2: "13:00" 3: "13:30" 4: "14:00" 5: "14:30" 6: "15:30" length: 7[[Prototype]]: Array(0) []0: "18:00" 1: "18:30" 2: "19:00" 3: "19:30" 4: "20:00" 5: "20:30" 6: "21:30" length: 7[[Prototype]]: Array(0)¿Cómo puedo agregar la hora de inicio incluso si termina con la manecilla de 30 minutos?
Su condición en el ciclo for debería ser i <= endH; y puede usar la condición if de acuerdo con su necesidad de salida.
for (var i = startH ; i <= endH; i++) {Por lo que entiendo, su resultado esperado es:
("11:30", "15:30") => ["11:00","11:30","12:00","12:30","13:00","13:30","14:00","14:30","15:00","15:30"] ("18:00", "21:30") => [ "18:00","18:30","19:00","19:30","20:00","20:30","21:00","21:30" ] //DIVIDING THE TIME FRAME IN 30 MINUTES OF THE TIME FRAME. function returnTimesInBetween(start, end) { var timesInBetween = ([]); console.log(timesInBetween); var startH = parseInt(start.split(":")[0]); var startM = parseInt(start.split(":")[1]); var endH = parseInt(end.split(":")[0]); var endM = parseInt(end.split(":")[1]); for (var i = startH ; i <= endH; i++) { timesInBetween.push(i < 10 ? "0" + i + ":00" : i + ":00"); (i == endH && endM == 30) ? timesInBetween.push(i < 10 ? "0" + i + ":30" : i + ":30") : ''; } return timesInBetween; } console.log('time range:-') console.log(returnTimesInBetween("11:30", "15:00")); console.log(returnTimesInBetween("11:30", "15:30")); console.log(returnTimesInBetween("18:00", "21:30"));O
si espera algo como esto, simplemente agregue la condición if:
["11:30","12:00","12:30","13:00","13:30","14:00","14:30","15:00","15:30"] ["18:00","18:30","19:00","19:30","20:00","20:30","21:00","21:30"] //DIVIDING THE TIME FRAME IN 30 MINUTES OF THE TIME FRAME. function returnTimesInBetween(start, end) { var timesInBetween = ([]); console.log(timesInBetween); var startH = parseInt(start.split(":")[0]); var startM = parseInt(start.split(":")[1]); var endH = parseInt(end.split(":")[0]); var endM = parseInt(end.split(":")[1]); if (startM == 30) { timesInBetween.push(startH < 10 ? "0" + startH + ":30" : startH + ":30"); startH++; } for (var i = startH ; i <= endH; i++) { timesInBetween.push(i < 10 ? "0" + i + ":00" : i + ":00"); (i == endH && endM == 30) ? timesInBetween.push(i < 10 ? "0" + i + ":30" : i + ":30") : ''; } return timesInBetween; } console.log('time range:-') console.log(returnTimesInBetween("11:30", "15:00")); console.log(returnTimesInBetween("18:00", "21:30"));Necesita una condición solo para la primera iteración, cuando starM no es 30.
//DIVIDING THE TIME FRAME IN 30 MINUTES OF THE TIME FRAME. function returnTimesInBetween(start, end) { var timesInBetween = []; var startH = parseInt(start.split(":")[0]); var startM = parseInt(start.split(":")[1]); var endH = parseInt(end.split(":")[0]); var endM = parseInt(end.split(":")[1]); for (var i = startH; i < endH; i++) { // Add a condition here for the first iteration only if (i === startH && startM !== 30) { timesInBetween.push(i < 10 ? "0" + i + ":00" : i + ":00"); } timesInBetween.push(i < 10 ? "0" + i + ":30" : i + ":30"); } if (endM == 0) timesInBetween.push(endH + ":30"); if (endM == 30) timesInBetween.push(endH + ":30"); return timesInBetween; // Was missing ;) } console.log(returnTimesInBetween("11:30", "15:30")); console.log(returnTimesInBetween("18:00", "21:30"));Otro enfoque que me gusta cuando se trabaja con el tiempo, en lugar de trabajar con la hora, el minuto (y el segundo) por separado, convertirlos en un solo número (en este caso, minutos) y hacer todos los cálculos con el número. Solo vuelva a convertir al formato legible por humanos cuando termine de computar. Así evitas equivocarte al cambiar un componente y olvidarte del otro. Esto también se aplica a otros sistemas que no sean de base 10.
function returnTimesInBetween(start, end) { let result = []; const s = start.split(":").reduce((a,c) => a * 60 + Number(c)); const e = end.split(":").reduce((a,c) => a * 60 + Number(c)); const mod = (s % 30); for(let i = s + (mod ? 30 - mod : 0) ; i <= e; i += 30) { const h = Math.floor(i/60); const m = i % 60; result.push(`0${h}`.slice(-2) + ":" + `0${m}`.slice(-2) ); } return result; } console.log(returnTimesInBetween("11:00","15:29")); console.log(returnTimesInBetween("11:00","15:30")); console.log(returnTimesInBetween("11:29","15:30")); console.log(returnTimesInBetween("11:30","15:30")); console.log(returnTimesInBetween("11:31","15:30"));