【发布时间】:2020-03-30 21:34:29
【问题描述】:
我正在尝试在 Angular 中制作日历。为此,我实现了一个拖放功能,以便能够将一个约会从一天移动到另一个约会。
但是我有一些奇怪的事情,当我尝试移动约会时它似乎有效,但是当我尝试将它从 4 月 1 日移动到 3 月 31 日时,日期被修改为 3 月 1 日。
当我放弃约会时,我会用我的约会数据和新的一天发出更改:
drop(evt) {
let schedule: Schedule;
schedule = evt.data.schedule;
// Emit change
this.scheduleChange.emit({schedule, day: this.day});
}
然后我编辑我的约会:
scheduleChanged(evt) {
const schedule = this.createScheduleFromObject(evt.schedule);
const day = evt.day;
console.log(day);
if (this.isSameDate(schedule.start, schedule.end)) {
schedule.start.setDate(day.getDate());
schedule.start.setMonth(day.getMonth())
schedule.start.setFullYear(day.getFullYear());
schedule.end.setDate(day.getDate());
schedule.end.setMonth(day.getMonth());
schedule.end.setFullYear(day.getFullYear());
console.log(schedule);
}
}
我认为问题在于我将对象转换为 Schedule 类时:
createScheduleFromObject(obj: any) {
const schedule: Schedule = Object.assign(new Schedule(null, '', '', '', new Date(), new Date()), obj);
console.log(obj.start);
schedule.start = new Date(obj.start);
schedule.end = new Date(obj.end);
console.log(schedule.start);
return schedule;
}
这个函数返回好的日期这里是控制台日志的输出:
2020-04-01T21:31:49.640Z
Wed Apr 01 2020 23:31:49 GMT+0200
但是当我在 scheduleChanged 函数中修改它时,即使那天是 3 月 31 日,就像我在控制台日志中看到的那样:
Tue Mar 31 2020 00:00:00 GMT+0200
我的日程安排的开始日期设置为:
Sun Mar 01 2020 23:33:19 GMT+0100
为什么?
【问题讨论】:
标签: javascript angular typescript date