Fullcalendar and timezones. Help, I'm doing it wrong

我是研究僧i 提交于 2019-12-05 00:16:06

You are serializing CDT-adjusted dates as UTC dates (thus getting a 5 hour shift) so when they are read back in they get re-adjusted to CDT, and so on..

Because there isn't a way to set a timezone on JS date objects, Fullcalendar represents them internally as UTC dates, but adjusts for timezone offset on input time.

$.fullCalendar.parseISO8601('2011-04-19T17:00:00.000-05:00');
// Tue Apr 19 2011 22:00:00 GMT+0000 (GMT)  <-- note time shift

This is why, when you serialize to JSON, you get a string with the "Zulu" (UTC) timezone:

var dt = $.fullCalendar.parseISO8601('2011-04-19T17:00:00.000-05:00');
JSON.stringify( dt ); // "2011-04-19T22:00:00.000Z"

You need the date back to your timezone. It doesn't look like Fullcalendar has this so you'll need to to the work:

// detect local timezone offset
var localoffset = (new Date()).getTimezoneOffset();
// "unadjust" date
ret = new Date( ret.valueOf() + (localoffset * 60 * 1000) );

// serialize
function pad (n) { return String(n).replace(/^(-?)(\d)$/,'$10$2'); }
JSON.stringify( ret )
     // replace Z timezone with current
     .replace('Z', pad(Math.floor(localoffset / 60))+':'+ pad(localoffset % 60));

// should result in something like: "2011-04-21T19:00:00.000-05:00"

There may be a better way of solving this using Fullcalendar but I am not familiar with it.

Code is untested: I live in GMT with no-DST and don't really want to mess with my system just to see it work (YMMW). :-)

I had the same timeshift in FullCalendar. Check out your timezone on server, when I changed it to my own it help me. You may try to do it in few ways:

On serer:

[root@mx ~]# mv /etc/localtime /etc/localtime.old

[root@mx ~]# ln -s /usr/share/zoneinfo/Europe/Moscow /etc/localtime

Or in PHP script (which returns JSON string):

date_default_timezone_set('Europe/Moscow');

P.S. Don't forget to change "Europe/Moscow" to your values :-)

Then you have to set your valid time in new time zone ("date" command);

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!