Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 日期在Chrome和Firefox中显示不一致_Javascript_Date_Jquery Ui Datepicker - Fatal编程技术网

Javascript 日期在Chrome和Firefox中显示不一致

Javascript 日期在Chrome和Firefox中显示不一致,javascript,date,jquery-ui-datepicker,Javascript,Date,Jquery Ui Datepicker,以下代码针对jQueryUIDatePicker运行。日期根据JSON响应是否包含该日期的数据突出显示。这在Chrome(32.0.1675.2金丝雀)中运行良好,但在Firefox中不起作用。有人知道为什么吗?突出显示类未添加到FF中 function( response ) { MyApp.events = response; //console.log(events[1]); $("#my-event-calendar

以下代码针对jQueryUIDatePicker运行。日期根据JSON响应是否包含该日期的数据突出显示。这在Chrome(32.0.1675.2金丝雀)中运行良好,但在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(新日期(event.Date.valueOf())呈现
138058200000
但在Firefox中这是
-1775005200000

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


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

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

在这种情况下,您似乎需要以下函数:

// 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要求的):


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

一个浏览器似乎将2013年1月10日视为1913年,另一个浏览器视为2013年。那应该是1月10日还是10月1日?像2013-10-01这样的格式就不那么模棱两可了。我们双方的另一个问题是——日期格式(MM/DD/YY或DD/MM/YY)模棱两可。是的,但是OP就是这样的。
// 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]));
}