0

以下代码针对 jQuery UI 日期选择器运行。根据 JSON 响应是否包含该日期的数据来突出显示日期。这在 Chrome (32.0.1675.2 canary) 中可以正常工作,但在 Firefox 中不行。有谁知道为什么会这样?FF 中没有添加高亮类。

function( response ) {
            MyApp.events = response;
            //console.log(events[1]);
            $("#my-event-calendar" ).datepicker({

                beforeShowDay: function(date) {

                    var result = [true, '', null];
                    var matching = $.grep(MyApp.events, function(event) {
                        //console.log(new Date(event.Date).valueOf() );
                        dateToHighlight = new Date(event.Date).valueOf();
                        return dateToHighlight === date.valueOf();
                    });

                    if (matching.length) {
                        result = [true, 'highlight', null];
                    }

                    return result;
                },

在 Chrome 中,console.log(new Date(event.Date).valueOf() );呈现,1380582000000但在 Firefox 中,这是-1775005200000

更新,JSON数据现在格式如下:

对象{日期:“2013-10-02T14:30:00+00:00”,标题:“事件标题”}

4

1 回答 1

0

正如 Quantas 人所说,让 Date 函数解析字符串并不是一个好主意。手动解析日期字符串要好得多。另外,不要使用 2 位数的年份。

在这种情况下,您似乎需要一个功能,例如:

// Expects date in format m/d/yy
// where all years are +2000
function parseEventDate(s) {
  s = s.split(/\D/);
  return new Date(+s[2]+2000, ++s[0], s[1]);
}

将日期字符串转换为日期对象。请注意,将根据客户端的系统设置创建日期对象。

编辑

您现在使用的是 ISO8601 格式,例如 2013-10-02T14:30:00+00:00。虽然这与 ES5 一致,但可能有 25% 的正在使用的浏览器不支持 Date.parse 方法(取决于您相信谁的统计数据),因此最好的方法仍然是手动解析字符串。

以下假设日期和时间是 UTC(这是 ES5 要求的):

// Expects date in format yyyy-mm-ddThh:mm:ssZ
// Assumes timezone offset is 0000
function parseEventDate(s) {
  s = s.split(/\D/);
  return new Date(Date.UTC(s[0], --s[1], s[2], s[3], s[4], s[5]));
}

如果要包括时区偏移量,则需要做更多的工作,但并不多。

于 2013-10-21T12:57:36.647 回答