Easy clamped time input

Easy clamped time input

I dont want invalid hours, but I dont want to allow invalid state, or require changing hours then minutes!

        const shuffleIn = (time, start, end) => {
            const cursor = time.clone();
            let moved = false;
            while(cursor.isBefore(start)) {
              cursor.add(1, 'day');
              moved = true;
            }
            while(cursor.isAfter(end)) {
              cursor.subtract(1, 'day');
              moved = true;
            }
            while(cursor.isBefore(start)) {
              cursor.add(1, 'hour');
              moved = true;
            }
            while(cursor.isAfter(end)) {
              cursor.subtract(1, 'hour');
              moved = true;
            }
            while(cursor.isBefore(start)) {
              cursor.add(1, 'minute');
              moved = true;
            }
            while(cursor.isAfter(end)) {
              cursor.subtract(1, 'minute');
              moved = true;
            }
            if(moved) {
              cursor.startOf('minute');
            }
            return cursor;
          }
    
Walk the input time into the correct range, in steps of increasing granularity.