2013-02-19 68 views
7

我有下面的代码,它可以正常工作,直到datepicker达到BST。jquery datepicker getTime not UTC

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的datepicker时间。

编辑:

从select.startdates选项的一个例子是

<option value="2013, 2, 1">01/03/2013</option> 
+0

的时间可能是局部的,我会去看看DOC – 2013-02-19 16:23:02

+0

@HugoDozois - 是啊...任何想法如何,我可以“unlocalize”吧? – Tom 2013-02-19 16:25:14

+0

@Tom:[Date](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date)对象的getTimezoneOffset方法以分钟返回偏移量。您可以使用它将本地日期转换为UTC。 – Martijn 2013-02-19 17:16:39

回答

2

它看起来像日期选择不返回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功能,但你不能与Date构造函数中使用apply

如果你宁愿离开你startDates阵列中的UTC,那么你需要的日期选择器的日期转换为UTC:

function showEventDates(date) { 
    date = date - new Date(date).getTimezoneOffset() * 60000; // convert local to UTC 
    // for ... 
} 

注意:选择一个其他的这些方法,而不是两个,或者你最终会遇到同样的问题... :-)

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一部分,但无法伤害。

0

基于@Martijn评论:

var offset = date.getTimezoneOffset(); 

if (date.getTime() - offset == startDates[i]) 
{ 
     return [true, 'eventDay']; 
}