如何将日期范围拆分为时间间隔?

时间:2014-06-10 19:58:18

标签: javascript jquery fullcalendar momentjs

我正在使用fullcalendar

通过选择数据范围,我可以检查所选范围内的项目数。

现在我需要将这些任务划分为时间间隔 - 根据日历间隔(15分钟) - 为了验证是否有2个事件彼此相邻。如果在同一时间间隔(事件1和事件2)中有2个事件,则用户不能选择此间隔。

怎么做?

http://jsfiddle.net/LFAzB/

$(document).ready(function() {

    $('#calendar').fullCalendar({
        header: {
            left: 'prev,next today',
            center: 'title',
            right: 'agendaDay'
        },
        defaultView: 'agendaDay',
        allDaySlot: false,
        scrollTime: '10:00:00',
        slotDuration: '00:15:00',
        minTime: '06:45:00',
        maxTime: '23:15:00',
        eventDurationEditable: true,
        eventStartEditable: true,

        selectable: true,
        selectHelper: true,
        events: [
            {
                title: 'Event 1',
                start: moment().format('YYYY-MM-DD') + 'T10:30:00',
                end: moment().format('YYYY-MM-DD') + 'T10:45:00'
            },
            {
                title: 'Event 2',
                start: moment().format('YYYY-MM-DD') + 'T10:30:00',
                end: moment().format('YYYY-MM-DD') + 'T10:45:00'
            },
            {
                title: 'Longer Event',
                start: moment().format('YYYY-MM-DD') + 'T12:30:00',
                end: moment().format('YYYY-MM-DD') + 'T13:45:00'
            },
            {
                title: 'Meeting',
                start: moment().format('YYYY-MM-DD') + 'T13:45:00',
                end: moment().format('YYYY-MM-DD') + 'T14:00:00'
            }
        ],
        select: function(start, end) {
            console.log( 'Events :' + getEventsByTime( start, end ).length );
            console.log( getEventsByTime( start, end ) );
        }
    });

});


function getEventsByTime( start, stop ) {
    var todaysEvents = $('div[name=calendar]').fullCalendar('clientEvents', function(event) {

       return ( 
           ( event.start >= start && event.end <= stop ) || 
           ( start >= event.start && stop <= event.end)  || 
           (start <= event.start && stop >= event.start) ||
           (start >= event.start && start <= event.end)
       );
    });
    return todaysEvents;
}

1 个答案:

答案 0 :(得分:1)

我知道了。她的解决方案:

http://jsfiddle.net/LFAzB/5/

select: function(start, end) {
    console.log( 'Events :' + getEventsByTime( start, end ).length );
    var ev = getEventsByTime( start, end );
    console.log( ev );
    var itms = {};            

    ev.forEach(function(entry){

      var begin = moment(entry.start);
      var final = moment(entry.end);


    while( begin.diff(final) < 0 ) {
        itms[begin] =  ( itms[begin] || 0) + 1;
        if( itms[begin] >= maxEventsInInterval ) {
            console.log(' __ WARNING __ , max events exceeded! ');
        }

        begin = moment(begin).add('seconds', 900);
    }

});
    console.log( itms );
}