且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Moment Js生成时隙

更新时间:2023-02-26 20:07:44

类似的方法应该起作用:

Something like this should work:

var x = {
    nextSlot: 30,
    breakTime: [
        ['11:00', '14:00'], ['16:00', '18:00']
    ],
    startTime: '8:00',
    endTime: '20:00'
};

var slotTime = moment(x.startTime, "HH:mm");
var endTime = moment(x.endTime, "HH:mm");

function isInBreak(slotTime, breakTimes) {
    return breakTimes.some((br) => {
      return slotTime >= moment(br[0], "HH:mm") && slotTime < moment(br[1], "HH:mm");
  });
}

let times = [];
while (slotTime < endTime)
{
  if (!isInBreak(slotTime, x.breakTime)) {
     times.push(slotTime.format("HH:mm"));
  }
  slotTime = slotTime.add(x.nextSlot, 'minutes');
}

console.log("Time slots: ", times);

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js"></script>