我有以下代码可以正常工作,直到datepicker达到BST。
var i;
function showEventDates(date) {
for (i = 0; i < startDates.length; i++) {
if (date.getTime() == startDates[i]) {
return [true, 'eventDay'];
}
}
return [false, ''];
}
var startDates = new Array();
$("select.startdates").find("option").each( function() {
startDates.push(Date.UTC.apply(Date, this.value.split(",").map(Number)));
});
$('#mydate').datepicker({
beforeShowDay: showEventDates
});
在BST期间,行if (date.getTime() == startDates[i]) {
返回false,因为有一个小时的差异。
我有什么想法可以让这些匹配?我认为这是不是UTC的日期选择时间。
编辑:
select.startdates的选项示例是
<option value="2013, 2, 1">01/03/2013</option>
答案 0 :(得分:2)
看起来,datepicker不返回UTC日期,而是返回本地日期(实际上是Javascript中的默认值)。
将构建的日期转换为当地时间:
$("select.startdates").find("option").each( function() {
var d = Date.UTC.apply(Date, this.value.split(",").map(Number));
d = d + new Date(d).getTimezoneOffset() * 60000; // convert UTC to local
startDates.push(d);
});
通常情况下,我会使用new Date(year, month, day)
构造函数而不是Date.UTC
函数,但您不能将apply
与Date
构造函数一起使用。
如果你宁愿将你的startDates数组保留为UTC,那么你需要将datepicker的日期转换为UTC:
function showEventDates(date) {
date = date - new Date(date).getTimezoneOffset() * 60000; // convert local to UTC
// for ...
}
注意:选择一个或这些方法中的另一个,而不是两者,或者你最终会遇到同样的问题......: - )
答案 1 :(得分:0)
我听说jQuery datetimepicker有一些时区更新,所以你可能想先检查网站,不过这是我做的选择日期和时间。 UTC格式的时间。
首先创建datetimepicker并使用城市而不是+0500 GMT,因为如果你使用GMT补偿你必须考虑夏令时 - 这是一场噩梦。
// create ye datetimepicker with timezone options
$('#datetimepicker').datetimepicker({
showTimezone: true,
onSelect: onSelect,
timezoneList: [
{ value: 'America/New_York', label: 'New York'},
{ value: 'America/Chicago', label: 'Chicago' } ,
{ value: 'America/Denver', label: 'Denver' },
{ value: 'America/Los_Angeles', label: 'Los Angeles' }
]);
接下来,从mde on Github获取timezoneJS.Date库(注意:您还需要为您所在的地区下载相应的时区文件,只需按照README说明进行操作)
现在,当用户选择日期时,会调用onSelect方法。
function onSelect(dateText, dateInst) {
// get the date without the timezone data
var d = $('#datetimepicker').datepicker('getDate');
// init timezoneJS
timezoneJs.timezone.zoneFileBasePath = '/tz';
timezoneJs.timezone.init();
// get the selected timezone
var tz = $('#datetimepicker').data('datepicker').settings.timepicker.timezone
// construct the utcDate with the help of the timezoneJS.Date lib
var utcDate = new timezoneJS.Date(
d.getFullYear(),
d.getMonth(),
d.getDate(),
d.getHours(),
d.getMinutes(),
tz)
var utcLinuxTimestamp = utcDate.getTime() / 1000
}
并非完全没有疼痛,但它可以为您节省日常用品。
使用UTC时间戳填充日期和时区的datetimepicker的反向如下所示:
// init timezone JS
timezoneJs.timezone.zoneFileBasePath = '/tz';
timezoneJs.timezone.init();
// get timezone date JS object
var tz = 'America/New York';
var d = new timezoneJS.Date( timestamp * 1000, tz);
$('#datetimepicker').datetimepicker({
showTimezone: true,
timezoneList: [
{ value: 'America/New_York', label: 'New York'},
{ value: 'America/Chicago', label: 'Chicago' } ,
{ value: 'America/Denver', label: 'Denver' },
{ value: 'America/Los_Angeles', label: 'Los Angeles' }
],
timezone: tz,
defaultDate: d._dateProxy,
onSelect: onSelect
}).datepicker('setDate',d._dateProxy);
我不确定你是否需要最后一行的setDate部分,但不会受到伤害。
答案 2 :(得分:0)
基于@Martijn评论:
var offset = date.getTimezoneOffset();
if (date.getTime() - offset == startDates[i])
{
return [true, 'eventDay'];
}