8

这是我在谷歌浏览器中的输出: 在此处输入图像描述

这是我在 Firefox 中的输出: 在此处输入图像描述

这是我的功能:

    function LoadSessions()
{ //LOAD SESSIONS FROM LOCALSTORAGE
    var retrievedObject = localStorage.getItem('session');

    // CALL FUNCTION
    parsePerObject(JSON.parse(retrievedObject));

    function parsePerObject(data)
    {
        // Turn the data object into an array
        var dataArray = [];
        $.each(data, function (key, value){
            dataArray.push(value);
        });

        // Sort data by starttime
        dataArray.sort(function (a, b) {
            if (a.starttime > b.starttime) return 1;
            if (a.starttime < b.starttime) return -1;
            return 0;
        });

        // Array with days of the week    
        var d_names = ["Sunday", "Monday", "Tuesday",
        "Wednesday", "Thursday", "Friday", "Saturday"];

        // Array with months of the year    
        var m_names = ["January", "February", "March", 
        "April", "May", "June", "July", "August", "September", 
        "October", "November", "December"];

        // Variables
        var content = "",
            dates = [], 
            date = {};

        // LOOP OBJECTS
        $.each(dataArray, function(i, item)
        {
            // Date
            var d = new Date(item.starttime);

            var year = d.getFullYear();
            var month = d.getMonth();
            var day = d.getDate();
            var da = d.getDay();

            date.value = year + "-" + month + "-" + day;

            // Startime & endtime
            var starttime = new Date(item.starttime);
            var endtime = new Date(item.endtime);

            var hour_starttime = starttime.getHours();
            var min_starttime = starttime.getMinutes();

            var hour_endtime = endtime.getHours();
            var min_endtime = endtime.getMinutes();

            // 12:00 instead of 12:0
            if(min_starttime < 10) min_starttime += "0";  
            if(min_endtime < 10) min_endtime += "0";

            // Loop dates
            $(date).each(function() 
            {
                // If the date is not in array dates
                if($.inArray(date.value, dates) == -1)
                {
                    date.firstchild = item.id;

                    content = "<div class='span6'><h2 id='" + item.id + "'" + " class='before-blocks'>" + d_names[da] + " " + day + " " + m_names[month] + " " + year + "</h2>";
                    content += "<ul class='sessionlist blocks unstyled'>";
                    content += "<li id='item" + item.id + "' class='contentblock has-thumb'>";
                    content += "<a href='/sessions/view/" + item.id + "'>";
                    content += hour_starttime + ":" + min_starttime + " - " + hour_endtime + ":" + min_endtime;
                    content += "<span class='ellipsis name' style='width: 95%;'><b>" + item.name + "</b></span>";
                    content += "<span class='ellipsis'><em>" + item.speaker + "</em></span></a></li>";
                    $("#here").append(content);

                    // Add date to dates 
                    dates.push(date.value);
                }

                // If the date is in array dates
                else
                {
                    var second = "<li id='item" + item.id + "' class='contentblock has-thumb'>";
                    second += "<a href='/sessions/view/" + item.id + "'>";
                    second += hour_starttime + ":" + min_starttime + " - " + hour_endtime + ":" + min_endtime;
                    second += "<span class='ellipsis name' style='width: 95%;'><b>" + item.name + "</b></span>";
                    second += "<span class='ellipsis'><em>" + item.speaker + "</em></span></a></li>";
                    $("#item" + date.firstchild).parent().append(second);    // Add li to parent (ul) of previous li
                }
                content = "</ul></div>";
                $("#here").append(content);
            });
        }); 
    }
}

在我的 localStorage 我有这样的对象:

{"21216":{"id":"21216","external_id":"","sessiongroupid":"1861","eventid":"5588","order":"0","name":"使用 ZF2 轻松实现 RESTful 服务","description":"需要了解 ZF2?并实现这些功能。","starttime":"2013-01-25 09:00:00","endtime":"2013 -01-25 12:30:00","speaker":"Matthew Weier O'Phinney, Rob Allen","location":"Tutorial Room","mapid":"0","xpos":"0.000000" ,"ypos":"0.000000","maptype":"plan","imageurl":"","presentation":"","organizer":"0","twitter":"","allowAddToFavorites":"0","allowAddToAgenda":"0","votes":"0","url":" http://conference.phpbenelux.eu/ " “地点”:“0”}}

有人知道为什么我没有时间在 Firefox 中显示 NaN:NaN 吗?

4

5 回答 5

10

如果您不想更改日期在内部的存储方式,并且不介意包含外部库,那么如果您提供格式,moment.js将为您正确解析这些字符串。请参阅文档进行解析(http://momentjs.com/docs/#/parsing/string-format/)和格式化(http://momentjs.com/docs/#/displaying/format/

var d = new moment(item.starttime, "YYYY-MM-DD HH:mm:ss");
// To get the date as 'Friday 25th January 2013'
var datestring = d.format("dddd Do MMMM YYYY");
// To get the time as '09:30'
var timestring = d.format("HH:mm");
于 2013-04-26T11:56:37.097 回答
3

根据MDN,'yyyy-MM-dd HH:mm:ss' 不是有效的日期格式,因为它接受 RFC2822 和 ISO 8601 格式。不是 Firefox 坏了,只是 Chrome 更宽容一些。要在 Firefox 中修复这种情况,请使用此格式“2013-01-25T09:00:00”。

于 2013-04-26T12:08:44.080 回答
3

您可以通过用“T”替换日期部分后的空格并在末尾附加“Z”来调整您的时间字符串以在 Firefox 中工作。这也适用于现代 webkit && IE9+ 浏览器。

较旧的 IE 需要更多...

var starttime='2013-01-25 12:30:00';
if(starttime.indexOf('Z')==-1)starttime=starttime.replace(' ','T')+'Z';

var date=new Date(starttime)

date.toUTCString()
/*  returned values: 
Firefox 20.0: Fri, 25 Jan 2013 12:30:00 GMT
Chrome 26.0.1410.64: Fri, 25 Jan 2013 12:30:00 GMT
MSIE 10.0: Fri, 25 Jan 2013 12:30:00 UTC
Opera 12.12: Fri, 25 Jan 2013 12:30:00 GMT
*/
于 2013-04-26T13:39:35.110 回答
3

而不是使用Date(string)构造函数使用Date(milliseconds)构造函数:http ://www.w3schools.com/js/js_obj_date.asp

原因是 Date(string) 将根据浏览器语言设置进行不同的解析。在您的情况下,它没有被 Firefox 解析(由于某些设置),因此是 NaN。当然,您还需要将内部存储表示更改为数字。

使用获取当前时间:

var time = (new Date()).getTime();

稍后使用

var date = new Date(time);

或者使用 moment.js:http ://momentjs.com/docs/

iff starttime 格式为 2013-01-25 09:00:00 您需要使用

var starttime = moment(item.starttime,"YYYY-MM-DD HH:mm:ss")

结束时间类似。

于 2013-04-26T11:37:08.677 回答
0

另一个与 chrom 无关的问题

// 12:00 instead of 12:0
if(min_starttime < 10) min_starttime += "0"; 

时间到了会发生什么1?你会得到10.

于 2013-04-26T12:06:48.803 回答