【发布时间】: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,您可能应该使用datetime或datetime2类型。 -
我应该先尝试更改为 datetime 吗?
-
天哪!更改为 datetime 将正确的日期存储在我的数据库中。但现在的问题是 FullCalender 没有读取 17/7/2013 12:00:00 AM 格式
标签: javascript jquery html time fullcalendar