如何动态地将1小时添加到事件的结束日期?



我想在事件的开始日期上增加 1 小时并将其设置为结束日期。每当我尝试使用moment的添加功能时,我都会得到:

捕获的类型错误:无法读取未定义的属性"_calendar" 在 D (moment.min.js:28) at e (jquery.min.js:4) at xb (jquery.min.js:4) at xb (jquery.min.js:4) at xb (jquery.min.js:4) at Function.r.param (jquery.min.js:4) at Function.ajax (jquery.min.js:4) at HTMLButtonElement.((索引):385) at HTMLButtonElement.dispatch (jquery.min.js:3) at HTMLButtonElement.q.handle (jquery.min.js:3)

下面是我的代码:

function loadCalendar(batch) {
    calendar.fullCalendar({
        defaultDate: moment("2016-12-11"),
        utc: true,
        header: false,
        columnFormat: 'dddd',
        allDaySlot: false,
        hiddenDays: [0],
        defaultView: 'agendaWeek',
        minTime: '07:00:00',
        maxTime: '21:00:00',
        editable: true,
        droppable: true,
        lazyFetching: true,
        eventOverlap: false,
        events: {
            editable: true
        },
        drop: function(event, ui) {
            classname = $(this).attr("id");  
        },
        eventReceive: function(event) {
//                                    classname = $(this).attr("id");
            console.log(classname);
            $("#new-subj").modal("show");
            $("#save-subj").click(function() {
//                                        var batch = $(".course").text();
                var subjcode = $("#subjcode").val();
                var start = event.start.format("YYYY-MM-DD[T]HH:mm:SS+08:00");
                var end = moment(start).add(1, "hour");
                var prof = $("#prof").val();
                var profname = $("#prof option:selected").text();
                var room = $("#room").val();
                var roomname = $("#room option:selected").text();
                var course = $("#course-hidden").val();
                var yrlvl = $("#yrlvl-hidden").val();
//                                        var classname = $(this).css("background-color");
                $.ajax({
                    url: eventurl,
                    type: "POST",
                    data: {
                        batch: batch,
                        subjcode: subjcode,
                        prof: prof,
                        start: start,
                        end: end,
                        room: room,
                        course: course,
                        yrlvl: yrlvl,
                        classname: classname,
                        action: "add"
                    },
                    dataType: "json",
                    success: function(data) {
                        event.id = data.eventid;
                        console.log(data.eventid);
                        event.title = subjcode;
                        event.description = profname + "<br/>" + roomname;
                        event.className = classname;
                        calendar.fullCalendar('updateEvent', event);
                        $("select").prop("selectedIndex", 0);
                    },
                    error: function(e){
                        console.log("adding event: "+e.responseText);
                    }
                });
                calendar.fullCalendar('updateEvent', event);
                $("#new-subj").modal("hide");
            });
            $("#cancel-subj").click(function() {
                calendar.fullCalendar('removeEvents', event.id);
                getEvents();
            });
        },
        eventDragStop: function (event, jsEvent, ui, view) {
            if (isElemOverDiv(jsEvent)) {
                swal({
                    title: "Are you sure you want to delete this subject?",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "",
                    confirmButtonText: "Delete",
                    closeOnConfirm: false
                },
                function(isConfirm) {
                    if (isConfirm) {
                        $.ajax({
                            url: eventurl,
                            type: "POST",
                            data: {
                                id: event.id,
                                action: "delete"
                            },
                            dataType: "json",
                            success: function(data) {
                                console.log(data);
                                if (data.status == "success") {
                                    console.log(event.id);
                                    setTimeout(function(){
                                        calendar.fullCalendar('removeEvents', event.id);
                                        getEvents();
                                        swal("Deleted", "The subject has been deleted.", "success");
                                    }, 2000);
                                }
                            },
                            error: function(e){ 
                                console.log('Error processing your request: '+e.responseText);
                            }
                        });
                    }
                });
            }
        }
    });
}

这应该可以解决问题。对于事件,请按如下所示设置事件对象的开始日期和结束日期:

event.end = event.start.add(moment.duration("01:00:00"));

如果您的 event.start 不是时刻对象,您可能需要执行event.end = moment(event.start) ...才能使添加工作。

我已经通过使用moment(event.start).add(1, "h").format("YYYY-MM-DDTHH:mm:ss");解决了我自己的问题。Moment 将日期读取为无效的 ISO 格式,因此我认为我需要对其进行格式化。

相关内容

  • 没有找到相关文章

最新更新