Вы могли бы просто создать список, содержащий все часы, и удалить n
th первые записи, где n
- текущее 24-часовое время, например:
var now = moment().startOf('hour');
var all_hours=['12pm', '1am', '2am', '3am', '4am', '5am', '6am', '7am', '8am', '9am', '10am', '11am', '12am', '1pm', '2pm', '3pm', '4pm', '5pm', '6pm', '7pm', '8pm', '9pm', '10pm', '11pm'];
var remaining_hours=all_hours.slice(parseInt(now.format("H")), all_hours.length-1);
При этом я предполагаю, что причина, по которой вы делаете цикл, заключается в том, что это MVCE , а не совсем то, что вы делаете в производстве. Итак, основываясь на вашем примере, следующее должно работать
// Get current hour
var now = moment().startOf('hour');
// Get the 24 hour time
var this_hr_int24=parseInt(now.format("H"));
// The list to contain the remaining hours
var remaining_hours=[];
// Initialize loop variables
next_hr_int24=this_hr_int24;
next_hr=now;
// While the number in next_hr_int24 is less then 24
while (next_hr_int24 < parseInt(moment().endOf('day').format("H"))) {
// Increase by 60
var count = 60;
// Next hour of day
next_hr = next_hr.add(count, 'minutes')
// Get the 24 hr time for the next hour
next_hr_int24 = next_hr.format("H")
// Get the am/pm value for the list
next_hr_apm = next_hr.format("h a")
remaining_hours.push(next_hr_apm);
}