日历与UTC和本地日期的混淆 [英] fullcalendar confusion with UTC and local date
问题描述
我确实让fullcalendar正常初始化.因此它代表当前日期.(午夜->午夜1天1小时时段)
I do let fullcalendar initialize normally. So it represents current date. (Midnight->midnight, 1day, 1h slots)
我从其他数据源获取带有时间戳的数据.格式为"YYYY-MM-DD HH:mm"(作为字符串传输,没有时区信息)
From some other datasource I get data with timestamps. The format is "YYYY-MM-DD HH:mm" (transmitted as a string, no timezone information)
因此,我将该字符串转换为矩对象,并针对fullcalendar.start和.end进行测试以查看其是否在其中.
So I convert that string to a moment object and test against fullcalendar.start and .end to see if it is within.
moment("2016-04-07 00:00") == $('#calendar').fullCalendar('getView').end
通过以下命令,结果为 false
This results in false though the following command
$('#calendar').fullCalendar('getView').end.format("YYYY-MM-DD HH:mm")
返回
"2016-04-07 00:00"
我也试图与diff
moment("2016-04-07 00:00").diff( $('#calendar').fullCalendar('getView').end,"minutes")
返回
120
对Chrome开发工具中的calendars.end对象的某些研究表明,该对象内部表示为
Some research on the calendars.end object in Chrome Dev Tools revealed that it internally is represented as
2016-04-07 02:00 GMT+0200
这对我来说很奇怪.我比格林尼治标准时间早2小时.所以应该正确地说2016-04-07 00:00 GMT + 0200,不是吗?这也解释了为什么上面的差异测试需要120分钟.
This looks strange to me. I am in timezone 2h ahead of GMT. So it should correctly say 2016-04-07 00:00 GMT+0200, should it not? This also explains why the diff test above resulted in 120 minutes.
有人可以帮忙吗?我不知道转换问题来自何处.我只使用没有时区信息的日期.如上所述,fullcalendar表示没有gotodate信息,并显示从00:00到00:00的时间条.那么为什么会有2h的差异呢?
Can someone help? I do not get where the conversion problem comes from. I am using only dates with no timezone information. And as said above, fullcalendar initalizes with no gotodate information and shows a time bar from 00:00 to 00:00. So why does it come that there is this 2h difference?
推荐答案
几件事:
-
timezone 参数控制FullCalendar如何与时区一起工作.
The timezone parameter controls how FullCalendar works with time zones.
默认情况下,FullCalendar使用模糊地带".这些是对fullCalendar中的moment.js的自定义.文档状态:
By default, FullCalendar uses "ambiguously-zoned moments". These are customizations to moment.js made within fullCalendar. The docs state:
矩对象也已扩展为表示没有指定时区的日期.在引擎盖下,这些时刻以UTC模式表示.
The moment object has also been extended to represent a date with no specified timezone. Under the hood, these moments are represented in UTC-mode.
因此,要在此模式下比较日期,请将其视为UTC时间.
Thus, to compare dates in this mode, treat them as if they were in UTC.
moment.utc("2016-04-07 00:00")
要比较时刻,请使用时刻查询功能, isSame
, isBefore
, isAfter
, isSameOrBefore
, isSameOrAfter
和 isBetween
.
在这种情况下,由于FullCalendar的开始日期是包含在内的,而结束日期是不包含,因此您可能需要这样比较:
In this case, since FullCalendar's start is inclusive but the end date is exclusive, you probably want to compare like this:
var cal = $('#calendar').fullCalendar('getView');
var start = cal.start;
var end = cal.end;
var m = moment.utc("2016-04-07 00:00"); // your input
var between = m.isSameOrAfter(start) && m.isBefore(end);
请注意,在将来的发行版中,时刻的 isBetween
功能有一个待定的增强功能,可让您控制独占性,但是目前 isBetween
完全包含在内,因此您必须使用此处显示的功能组合.
Note that there's an pending enhancement to moment's isBetween
functionality for a future release that will give you control of exclusivity, but currently isBetween
is fully inclusive, so you have to use the combination of functions shown here.
这篇关于日历与UTC和本地日期的混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!