小编Mr.*_*hat的帖子

使用 Moment Js 生成时间段

我有这样的对象结构

var x = {
    nextSlot: 30,
    breakTime: [
        ['11:00', '14:00'], ['16:00', '18:00']
    ],
    startTime: '8:00',
    endTime: '20:00'
}
Run Code Online (Sandbox Code Playgroud)

我想生成从 startTime 到 endTime 的时间段。但我不想考虑时间段中的breakTime。输出应该是

['08:00','08:30','09:00','09:30','10:00','10:30','14:00','14:30','15:00','15:30', '17:30', '18:00', '18:30','19:00','19:30']
Run Code Online (Sandbox Code Playgroud)

我实现了我自己的逻辑。但这适用于;y 对于长度为 1 的breaktime 数组。

// Check whether the startTime is less than endTime
while (moment(x.startTime, ['HH:mm']).format('HH:mm') < moment(x.endTime, ['HH:mm']).format('HH:mm')) {
    for (let i = 0; i < x.breakTime.length; i++) {
        // if startTime is greater then breakTime[i][0], and if starttime is less then breaktime[i][1], 
        //just add nextSlot to starttime 
        if (moment(x.startTime, …
Run Code Online (Sandbox Code Playgroud)

javascript loops timeslots momentjs

1
推荐指数
1
解决办法
3746
查看次数

标签 统计

javascript ×1

loops ×1

momentjs ×1

timeslots ×1