2011-04-20 29 views
12

我做错了莫名其妙。我在Fullcalendar时区被绊倒。我试过设置ignoreTimezone为真和假,但它似乎并不重要。它在两个地方的代码中,因为我不确定它是从哪里去的。Fullcalendar和时区。帮助,我做错了

我的数据源是一个隐藏的表单字段。 输出的数据FullCalendar通过增加5小时(CDT)进行调整。 FullCalendar中的数据不会通过删除5小时进行调整。

在后端,我只是节能而不处理它返回的JSON字符串(甚至对其进行解码)

Page Load: 
    Data In: Empty, no data 
    Data Edit: drag from noon to 2pm (CDT), then submit form 
    Data Out: Use clientEvent to get data, and JSON.stringify to put into form field. 
    [{"id":6844,"title":"Open","start":"2011-04-19T17:00:00.000Z","end":"2011-04-19T19:00:00.000Z","allDay":false}] 

Page Load (after submitting form): 
    Data In: Use JSON.parse to load data from hidden form field. This is the incoming data, but the event is shifted to 5pm (CDT) in the control. 
    [{"id":6844,"title":"Open","start":"2011-04-19T17:00:00.000Z","end":"2011-04-19T19:00:00.000Z","allDay":false}] 
    Data Out: Without changing the control, it's now: 
    [{"id":6844,"title":"Open","start":"2011-04-19T22:00:00.000Z","end":"2011-04-20T00:00:00.000Z","allDay":false}] 

我设置了Fullcalendar这样的:

// Fullcalendar for business hours page 

jQuery(document).ready(function() { 

    jQuery('#edit-submit').bind("click", business_hours_set); 
    jQuery('#edit-preview').bind("click", business_hours_set); 

    jQuery('#calendar').fullCalendar({ 

    // configure display 
    header: { 
     left: '', 
     center: '', 
     right: '' 
    }, 
    ignoreTimezone: false, 
    defaultView: 'agendaWeek', 
    allDaySlot: false, 
    firstHour: 8, 

    // configure selection for event creation 
    selectable: true, 
    selectHelper: true, 
    select: business_hours_add, 

    // configure data source 
    editable: true, 
    eventSources: [ 
    { 
     events: jQuery.parseJSON(jQuery('#fullcalendar_data').val()), 
     color: '#992B0A', 
     textColor: 'white', 
     ignoreTimezone: false 
    } 
    ], 

    // configure editing 
    eventClick: function(calEvent) { 
     business_hours_delete(calEvent.id); 
    } 
    }); 
    alert(jQuery('#fullcalendar_data').val()); 
}); 

function business_hours_add(startDate, endDate) { 
    var calendar = jQuery('#calendar'); 
    var newid = Math.ceil(Math.random()*64000); 
    calendar.fullCalendar('renderEvent', 
    { 
    id: newid, 
    title: "Open", 
    start: startDate, 
    end: endDate, 
    allDay: false 
    }, 
    true // make the event "stick" 
); 
    calendar.fullCalendar('unselect'); 
} 

var business_hours_selectedId = -1; 
function business_hours_delete(id) { 

    business_hours_selectedId = id; 

    jQuery("#dialog-confirm").dialog({ 
    resizable: false, 
    height:160, 
    modal: true, 
    buttons: { 
     "Yes, delete!": function() { 
     calendar = jQuery('#calendar'); 
     calendar.fullCalendar('removeEvents', business_hours_selectedId); 
     jQuery(this).dialog("close"); 
     }, 
     Cancel: function() { 
     jQuery(this).dialog("close"); 
     } 
    } 
    }, id); 
} 

function business_hours_set() { 
    var data = jQuery('#calendar').fullCalendar('clientEvents'); 

    // data is cyclical. Create a new data structure to stringify. 
    var ret = []; 
    for(var i=0; i<data.length; i++) { 
    var datum = { 
     id: data[i].id, 
     title: data[i].title, 
     start: data[i].start, 
     end: data[i].end, 
     allDay: data[i].allDay 
    } 
    ret[i] = datum; 
    } 
    // stringify and return 
    jQuery('#fullcalendar_data').val(JSON.stringify(ret)); 
    alert(JSON.stringify(ret)); 
} 

我究竟做错了什么?

在此先感谢,迈克

+0

麦克你有没有解决这个问题我也面临同样的问题 – Devjosh 2011-12-06 11:19:18

回答

2

您序列化CDT调整日期为UTC时间(从而得到5小时轮班),所以当他们读回,他们得到重新调整,以CDT,等等。

因为没有办法在JS日期对象上设置时区,Fullcalendar在内部将它们表示为UTC日期,但会在输入时调整时区偏移量。

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

这就是为什么,当你序列化JSON,你得到的“祖鲁”(UTC)时区的字符串:

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

您需要追溯到您的时区。它看起来并不像Fullcalendar有这个,所以你需要的工作:

// 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" 

有可能是解决这个使用Fullcalendar一个更好的方式,但我不熟悉它。

代码是未经测试的:我住在格林威治标准时间,没有DST,并且不想为了看到它工作而乱搞我的系统(YMMW)。 :-)

+0

嗨,谢谢你的回复。我了解(一点)时区问题,但不了解FullCalendar的行为。 我要放入完整日历的数据是祖鲁时间: [{ “ID”:6844, “标题”: “打开”, “开始”: “2011-04-19T17:00:00.000Z”, “端”: “2011-04-19T19:00:00.000Z”, “allDay”:false}] 但它似乎也调整了5个小时!将它从22:00开始。 我基本上把我从FullCalendar得到的数据提交回来,导致这个5小时的转变。 – Mike 2011-04-20 15:54:26

2

我在FullCalendar中有相同的时间转换。 在服务器上查看您的时区,当我将其更改为我自己的时区时,它可以帮助我。 您可以尝试做到在几个方面:

在塞雷尔语:

[根@ MX〜]#MV的/ etc /本地时间的/ etc /本地时间。老

[根@ MX〜]#LN -s在/ usr/share/zoneinfo中/欧洲/莫斯科的/ etc /本地时间

或者在PHP脚本(返回JSON字符串):

date_default_timezone_set('Europe/Moscow');

P.S. 不要忘记将“欧洲/莫斯科”更改为您的值:-)

然后,您必须在新时区(“日期”命令)中设置有效时间;

相关问题