2011-04-20 80 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. 不要忘記將「歐洲/莫斯科」更改爲您的值:-)

然後,您必須在新時區(「日期」命令)中設置有效時間;