Fullcalendar wrong time in on click event

守給你的承諾、 提交于 2019-12-04 07:38:58

The current version of FullCalendar uses moment.js extensively. The problems you are describing are from improper use of moment objects. Specifically:

$scope.start = event.toDate();

When you call toDate, you get back a regular JavaScript Date object - which will be based on the same UTC instant, but will always take on the behavior of the time zone where the code is running. You have a few options:

  • You can keep it as a moment object:

    $scope.start = event;
    
  • You can format it to an ISO string that retains the time zone offset:

    $scope.start = event.format(); // ex: '2015-03-04T08:00:00+01:00'
    

With either option, you'll need to follow through with the usage of $scope.start to expect either a moment or a string, rather than a Date.

Also, in this code:

$scope.day = new Date($filter('date')(event._d, "yyyy-MM-dd"));

You should never access the _d property directly, as there can be unintended side-effects. Consider the underscore to mean "internal". Also, you don't need Angular's date filter here since moment objects have formatting capabilities. Instead, just do this:

$scope.day = event.format("YYYY-MM-DD");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!