javascript - 全日历和时区。救命,我做错了

标签 javascript jquery timezone fullcalendar

不知何故我做错了。我在使用 Fullcalendar 时遇到了问题。我已经尝试将 ignoreTimezone 设置为 true 和 false,但这似乎无关紧要。它在下面的代码中有两个地方,因为我从文档中不确定它的位置。

我的数据源是一个隐藏的表单域。 FullCalendar 的数据通过添加 5 小时 (CDT) 进行调整。 inFullCalendar 的数据不会通过删除 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));
}

我做错了什么?

提前致谢,迈克

最佳答案

您将 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 可能有更好的方法来解决这个问题,但我不熟悉它。

代码未经测试:我住在格林威治标准时间,没有夏令时,真的不想为了看看它能正常工作而弄乱我的系统 (YMMW)。 :-)

关于javascript - 全日历和时区。救命,我做错了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5724142/

相关文章:

javascript - 如何使用 MaterializeCss 从多重选择中动态获取选定值

java - 如何从 Java 语言环境的时区获取日期模式

javascript - 从 Bokeh Google map 包装器调用 Javascript 方法 (fitBounds())

javascript - PHP - 如果用户退出网页则取消链接文件?

javascript - 放置事件后在 div 中计算 <img>

c# 将字符串(不带时区)解析为 DateTime,同时考虑 DST

java - 如何在 Joda Time/Java 8 中列出时区偏移量、时区 ID 和长名称?

javascript - 为什么 jQuery UI 布局在 Adob​​e AIR 中运行得这么慢?

javascript - 我如何打印用户在javascript中点击的字母?

javascript - jQuery:使用 .find 和 .addClass