I am generating the following array :
Current output:
["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", "21:45", "22:00", "22:15", "22:30", "22:45", "23:00", "23:15", "23:30", "23:45"]
The code :
var startTime = moment().utc().set({hour:11,minute:00});
var endTime = moment().utc().set({hour:23,minute:59});
var timeStops = [];
while(startTime <= endTime){
timeStops.push(new moment(startTime).format('HH:mm'));
startTime.add(15, 'minutes');
}
console.log('timeStops ', timeStops)
The problem :
Currently, everything works fine and as expected; however, I have a kind of logical issue.
Let's suppose that the endtime is 2:00 am 2 hours after midnight, knowing that I am following the 24hours representation (as this is what required).
In this case it doesn't push anything on the array as it thinks that the endtime is before the start time; however, it's the endtime of the next day.
By the way, from the backend I receive the following :
[11:00, 02:00] meaning [openingTime, closingTime]
I am generating the following array :
Current output:
["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", "21:45", "22:00", "22:15", "22:30", "22:45", "23:00", "23:15", "23:30", "23:45"]
The code :
var startTime = moment().utc().set({hour:11,minute:00});
var endTime = moment().utc().set({hour:23,minute:59});
var timeStops = [];
while(startTime <= endTime){
timeStops.push(new moment(startTime).format('HH:mm'));
startTime.add(15, 'minutes');
}
console.log('timeStops ', timeStops)
The problem :
Currently, everything works fine and as expected; however, I have a kind of logical issue.
Let's suppose that the endtime is 2:00 am 2 hours after midnight, knowing that I am following the 24hours representation (as this is what required).
In this case it doesn't push anything on the array as it thinks that the endtime is before the start time; however, it's the endtime of the next day.
By the way, from the backend I receive the following :
[11:00, 02:00] meaning [openingTime, closingTime]
Share
Improve this question
edited Jan 5, 2018 at 0:14
Hamza L.
asked Jan 4, 2018 at 23:54
Hamza L.Hamza L.
1,8234 gold badges27 silver badges47 bronze badges
3 Answers
Reset to default 10You can simply check if endTime
is before startTime
using isBefore
, then you can add
one day to make endTime
represent the next day.
Here a live sample:
function getTimeStops(start, end){
var startTime = moment(start, 'HH:mm');
var endTime = moment(end, 'HH:mm');
if( endTime.isBefore(startTime) ){
endTime.add(1, 'day');
}
var timeStops = [];
while(startTime <= endTime){
timeStops.push(new moment(startTime).format('HH:mm'));
startTime.add(15, 'minutes');
}
return timeStops;
}
var timeStops = getTimeStops('11:00', '02:00');
console.log('timeStops ', timeStops);
timeStops = getTimeStops('11:00', '23:59');
console.log('timeStops ', timeStops);
<script src="https://cdnjs.cloudflare./ajax/libs/moment.js/2.20.1/moment.min.js"></script>
Here a similar issue, with a similar solution.
I would add date into the equation.
If you know you are going to go to 2am, then add the date to push it to the next day. You don't even necessarily need it to be the correct date, just one day in the future from start:
var startTime = moment().utc().set({day: 1, hour:11, minute:00});
var endTime = moment().utc().set({day: 2, hour:2, minute:59});
This should allow you to properly go from some time the previous day to an end time the next day.
You can use moment-range iteration by (hours, minutes ....).
example:
const range = moment.range('2018-01-01 00:00', '2018-01-01 05:30');
const hours = Array.from(range.by('hour', { excludeEnd: true }));
hours.length == 5 // true
hours.map(m => m.format('HH:mm')) // ['00:00', '01:00', '02:00', '03:00', '04:00']