I have a function returnTimesInBetween. It's taking two values (start, end) as parameters and returns the time array by splitting in the 30 minutes of the time frame. the problem is when the start time is ending with 30 minutes like "12:30,11:30,15:30" it's not taking that value in the time frame and when the start time is like 11:00, 12:00, etc it's working fine.
//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"));
console output are
[]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)
How can i add the starting time even if it's ending with 30 minute hand.
CodePudding user response:
You need a condition just for the first iteration, when starM
is not 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"));
CodePudding user response:
Your condition on the for loop should be i <= endH;
and you can use th if condition according to your need of the output.
for (var i = startH ; i <= endH; i ) {
From what i understand your expected output is:
("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");
timesInBetween.push(i < 10 ? "0" i ":30" : i ":30");
}
return timesInBetween;
}
console.log('time range:-')
console.log(returnTimesInBetween("11:30", "15:30"));
console.log(returnTimesInBetween("18:00", "21:30"));
Or
if you are expecting somthing like this, just add the if condition:
["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");
timesInBetween.push(i < 10 ? "0" i ":30" : i ":30");
}
return timesInBetween;
}
console.log('time range:-')
console.log(returnTimesInBetween("11:30", "15:30"));
console.log(returnTimesInBetween("18:00", "21:30"));
CodePudding user response:
Another approach I like when working with time, instead of working with hour, minute (and second) separately, crunch them into a single number (in this case minutes) and do all calculation with the number. Only convert back to human-readable format when finish computing. This way you avoid making mistake when changing one component and forget the other. This applies to other non-base 10 system as well.
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"));
CodePudding user response:
Just for fun I wanted to see if I could produce a compact method to achieve this. This method uses a lot of shorthand syntax and could be difficult to follow. However, the premise is to convert the times into decimals, ie: 11:30 becomes 11.5, 21:00 becomes 21. The
preceding the h
and m
, in the reduce
function, is shorthand to convert the string to a number.
We can then define an array of a given size by deducting the endtime-decimal from our starttime-decimal and multiplying by 2. Then we fill the array with incrementing 0.5 decimal numbers from 11.5, 12, 12.5, 13...
Finally map
the array to convert back to a time-string format. Again this uses techniques like the bit operator |
which return a hole number and the modulo operator (also called remainder) %
.
let splitTime = (start, end) => {
let shm = start.split(":").reduce((h,m) => h ( m/60)) -0.5,
ehm = end.split(":").reduce((h,m) => h ( m/60));
return Array((ehm - shm) * 2).fill().map(_ => shm =0.5)
.map(hm => (hm | 0) ':' (hm % 1 ? "30" : "00"));
}
console.log(splitTime("11:30", "15:30"));
console.log(splitTime("18:00", "21:30"));
console.log(splitTime("9:00", "23:30"));