簡體   English   中英

Fullcalendar和時區。 救命,我做錯了

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

我不知何故做錯了。 我正在使用Fullcalendar絆倒時區。 我已經嘗試將ignoreTimezone設置為true和false,但似乎並不重要。 這是在下面的代碼中的兩個地方,因為我不確定它來自哪個文檔。

我的數據源是隱藏的表單字段。 超出 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));
}

我究竟做錯了什么?

先謝謝你,邁克

我在FullCalendar中有相同的時間轉換。 在服務器上查看你的時區,當我把它更改為我自己的時候它幫助我。 您可以嘗試以幾種方式執行此操作:

在serer上:

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

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

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

date_default_timezone_set( '歐洲/莫斯科');

PS別忘了將“歐洲/莫斯科”改為你的價值觀:-)

然后你必須在新的時區設置你的有效時間(“日期”命令);

您正在將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時,你得到一個帶有“Zulu”(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的GMT,並且真的不想弄亂我的系統只是為了看它工作(YMMW)。 :-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM