【问题标题】:FullCalendar Date differs when stored and retrieved from databaseFullCalendar 日期在从数据库中存储和检索时有所不同
【发布时间】:2013-07-14 19:14:11
【问题描述】:

我正在使用 FullCalendar,并且我的事件对象的开始日期存储在数据库中

copiedEventObject.start = date;

我稍后检索它并使用以下命令初始化我的日历:

        $.ajax({
            type: "POST",
            url: "Default.aspx/getCalendarEventsData",
            data: {},
            contentType: "application/json",
            dataType: "json",
            success: function (msg) {
                // Replace the div's content with the page method's return.

                calendarStoredEvents = $.map(msg.d, function (item) {
                    return {
                        title: item.EventTitle,
                        start: item.start,
                        id: item.EventID,
                        staffID: item.staffID
                    };
                });
                initializeCalendar(calendarStoredEvents);
            }
        });

item.start 在哪里:

2013-06-30T16:00:00.000Z

现在的问题是。 如果我在某个星期三存储了我的事件对象,它会检索并改为在星期二显示。 我读了一点,它与时区有关。我已经尝试添加:

ignoretimezone = false(失败,然后尝试将其切换为 true)

ignoretimezone = true(同样失败,然后尝试添加时区)

currentTimezone: 'Asia/Singapore'(效果不佳)

这是存储在数据库中的值:

            drop: function (date, allDay) { // this function is called when something is dropped

                // retrieve the dropped element's stored Event Object
                var originalEventObject = $(this).data('eventObject');

                // we need to copy it, so that multiple events don't have a reference to the same object
                var copiedEventObject = $.extend({}, originalEventObject);

                // assign it the date that was reported
                copiedEventObject.start = date;
                copiedEventObject.allDay = allDay;

                // render the event on the calendar
                // the last `true` argument determines if the event "sticks" (http://arshaw.com/fullcalendar/docs/event_rendering/renderEvent/)
                $('#calendar').fullCalendar('renderEvent', copiedEventObject, true);

                // is the "remove after drop" checkbox checked?
                if ($('#drop-remove').is(':checked')) {
                    // if so, remove the element from the "Draggable Events" list
                    $(this).remove();
                }
                // write to database
                var eventID = copiedEventObject.id;
                var staffID = copiedEventObject.staffID;
                var start = copiedEventObject.start;
                var end = copiedEventObject.end;
                var allDay = copiedEventObject.allDay;

编辑

我的网站托管在 Azure 中,所以我猜时间来自服务器:

当我删除一个事件时

(drop: 函数(date, allDay))

我使用了警报(开始)并显示(我的客户端时间)

2013 年 7 月 17 日星期三 00:00:00 GMT+0800(马来半岛标准时间)

我在后面的 C# 代码中写入数据库:

    string strConnectionString = ConfigurationManager.ConnectionStrings["PCSDB"].ConnectionString;
    SqlConnection conn = new SqlConnection(strConnectionString);
    conn.Open();
    using (SqlCommand sqlCommand = new SqlCommand("INSERT INTO CalendarData (EventID, staffID, start) VALUES (@EventID, @staffID, @start)", conn))
    {
        sqlCommand.Parameters.AddWithValue("@EventID", eventID);
        sqlCommand.Parameters.AddWithValue("@staffID", staffID);
        sqlCommand.Parameters.AddWithValue("@start", start);

        int queryResult = sqlCommand.ExecuteNonQuery();
    }
    conn.Close();

当我检查数据库时,起始值变为

2013-07-16T16:00:00.000Z

编辑 更改为日期时间后,我在数据库中保存了正确的日期。

现在我在为对象转换它时遇到问题。我用过:

  var parsedDate = $.fullCalendar.parseDate(item.start);
  var formatedDate = $.fullCalendar.formatDate(parsedDate, "dd-MM-yyyy");

传入

17/7/2013 12:00:00 AM 变为 07-05-2014

【问题讨论】:

  • 数据库中日历日期的数据类型是什么?您是否在将日期保存到数据库之前对其进行了任何转换?
  • nvarchar,我只是传入变量并保存了我得到的东西
  • @BloopieBloops - 你真的将日期存储到 nvarchar 类型中吗?那不是很好。你用的是什么数据库?日期总是有更好的类型。如果是 SQL Server,您可能应该使用 datetimedatetime2 类型。
  • 我应该先尝试更改为 datetime 吗?
  • 天哪!更改为 datetime 将正确的日期存储在我的数据库中。但现在的问题是 FullCalender 没有读取 17/7/2013 12:00:00 AM 格式

标签: javascript jquery html time fullcalendar


【解决方案1】:

时间在数据库中的存储方式无关紧要。重要的是时间如何通过您的 API 传输到 FullCalendar。假设您提供的确切字符串 "2013-06-30T16:00:00.000Z" 是传递给 FullCalendar 的内容,那么您描述的行为是有道理的。您提供的 UTC 时间正在调整为用户的本地时区。

FullCalendar 在查看者的本地时区显示日历。没有设置以 UTC 或任何其他时区查看日历。有一些信息here 他们提到“时区不可知论”。您可以从如何绘制日历的角度来考虑这种方式,但在查看任何 Date 对象时,您始终会使用本地时区,因为 JavaScript 就是这样工作的。

你有一些选择:

  • ignoreTimezone 选项设置为true,并将您的输入值作为字符串而不是日期传递。
  • 将值作为不带时区偏移的字符串传递(删除Z
  • 将值作为字符串传递,并为查看数据的人提供正确的时区偏移量。
  • 将值作为已针对本地时区进行调整的Date 对象传递。

currentTimezone 选项不是 FullCalendar 的一部分。它位于 gcal.js 插件中,用于通过 gcal 1.0 api 将 FullCalender 连接到 Google 日历。它只是将该参数作为查询字符串中的ctz 传递给Google。它只会影响从 Google 返回数据的方式。它在 FullCalendar 本身中没有任何作用。

【讨论】:

  • 我已将 ignoreTimezone all 设置为 true,尝试从时区偏移中删除 Z,但两者都不起作用。当我在第 16 天将我的活动放入日历时,它已经在我的数据库中写入第 15 天 - @Matt Johnson
  • 问题可能出在服务器端。请准确地向我们展示此代码产生的值,以及它是如何被接收和存储在数据库中的。 (编辑您的问题)
【解决方案2】:

您必须先将日期转换为formatDate 格式,然后才能将其保存到数据库。该事件将只接受 dd-MM-yyyy 00:00:00 之类的格式。

我的 select 方法中有这个,所以您可能没有正确获取事件对象。 这对我来说很好。

select: function(start, end, allDay){

      var startdate = $.fullCalendar.formatDate(start, "dd-MM-yyyy");
      var enddate = $.fullCalendar.formatDate(end, "dd-MM-yyyy");     

      alert(startdate);
      alert(enddate);
 }

这将显示类似 15-07-2013 的日期。

【讨论】:

  • C.I 添加了 var start = $.fullCalendar.formatDate(copiedEventObject.start, 'dd-MM-yyyy 00:00:00');在将其写入数据库之前,甚至没有出现检索时
  • 如果你错过了我很抱歉理解 00:00:00 我的意思是 hh:mm:ss :P
  • 嘿,我更新了帖子,我尝试了 formatDate 但年份变成了 2014 或 2015
猜你喜欢
  • 2016-01-23
  • 1970-01-01
  • 1970-01-01
  • 2014-01-18
  • 1970-01-01
  • 2011-12-30
  • 1970-01-01
  • 2013-08-20
  • 2015-06-20
相关资源
最近更新 更多