Supposedly, I have a start and end time.
Here is an example:
var startTime = '12:30 PM';
var endTime = '03:00 AM';
Now, I need to show the time between that range with a 5-minute interval, add 3 hours to the start time and subtract 1 hour from the end time. The result based on the given time range will be:
03:30 PM
03:35 PM
03:40 PM
03:45 PM
03:50 PM
03:55 PM
04:00 PM
04:05 PM
....
....
02:00 AM
The first time displayed will be 03:30 PM
because 12:30 PM + 3 hours = 03:30 PM
And the last time displayed will be 02:00 AM
because 03:00 AM - 1 hour = 02:00 AM
I am using moment.js. Here is my code:
var startTime = '12:30 PM';
var endTime = '03:00 AM';
var startTime2 = '12:30 PM';
var endTime2 = '05:00 PM';
console.log(intervals(startTime, endTime)); // this is not working
console.log(intervals(startTime2, endTime2)); // this is working
function intervals(start, end) {
var start = moment(start, 'hh:mm a').add(3, 'h');
var end = moment(end, 'hh:mm a').subtract(1, 'h');
var result = [];
var current = moment(start);
while (current <= end) {
result.push(current.format('hh:mm a'));
current.add(5, 'minutes');
}
return result;
}
My problem is that the given time
var startTime = '12:30 PM';
var endTime = '03:00 AM';
does not work. It only works if the given time is
var startTime2 = '12:30 PM';
var endTime2 = '05:00 PM';
I believe the issue lies in the PM-AM periods. How can we resolve this? Your help would be appreciated.