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

cld4siwp  于 2023-01-12  发布在  jQuery
关注(0)|答案(2)|浏览(85)

我使用的是fullcalendar
通过选择数据的范围,我可以检查有多少项目是在选定的范围。
现在,我需要根据日历间隔(15分钟)将这些任务划分为时间间隔,以验证是否有2个事件彼此相邻。如果在同一时间间隔内有2个事件(事件1和事件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;
}
zazmityj

zazmityj1#

我知道了。她的解决办法是:
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 );
}
guicsvcw

guicsvcw2#

下面的函数将start_date和end_date作为必需参数,这些参数将位于date对象中,可选参数(天间隔:dy_inv,小时间隔:hr_inv,分钟间隔:min_inv)将基于要拆分范围的间隔值。默认情况下,它将按1天拆分

function get_date_intervals(start_date, end_date, dy_inv=1, hr_inv=0, min_inv=0){
    var date_intervals = [];
        
    if (dy_inv == 0 && hr_inv == 0 && min_inv == 0){
        dy_inv = 1
    }

    sd = start_date
    while(sd < end_date){
        date_interval = [];
        date_interval.push(sd)

        let ed = new Date(sd.getFullYear(), sd.getMonth(), sd.getDate()+dy_inv, sd.getHours()+hr_inv, sd.getMinutes()+min_inv)
    
        if (ed >= end_date){
            ed = end_date
        }
        
        date_interval.push(ed)
        date_intervals.push(date_interval);

        sd = new Date(ed.getFullYear(), ed.getMonth(), ed.getDate(), ed.getHours(), ed.getMinutes()+1)
    }

    return date_intervals
}


// below is the example of function call, 
// Note: month will start from 0, i.e., Jan-0, Feb-1, Mar-2

// get_date_intervals(new Date(from_yr, from_mon-1, from_dy, from_hr, from_min), new Date(to_yr, to_mon-1, to_dy, to_hr, to_min), 0, 1, 0)
get_date_intervals(new Date(2022, 1-1, 1, 10, 5), new Date(2022, 3-1, 10, 5, 10), 0, 1, 0)

相关问题