1

我正在使用漂亮的日期 js,它将日期对象显示为“2 分钟前”、“5 天前”等社交方式。但是虽然它适用于 chrome,但它不适用于 Firefox,因为我是 javascript 新手,我不知道为什么. 从 J.Resig 页面获取代码并根据我的需要进行修改。

这是代码

// Takes an ISO time and returns a string representing how
// long ago the date represents.
function prettyDate(time){
    var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")),
        diff = (((new Date()).getTime() - date.getTime()) / 1000),
        day_diff = Math.floor(diff / 86400);

    if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
        return;

    return day_diff == 0 && (
            diff < 60 && "şimdi" ||
            diff < 120 && "1 dk önce" ||
            diff < 3600 && Math.floor( diff / 60 ) + " dk önce" ||
            diff < 7200 && "1 saat önce" ||
            diff < 86400 && Math.floor( diff / 3600 ) + " saat önce") ||
        day_diff == 1 && "Dün" ||
        day_diff < 7 && day_diff + " gün önce" ||
        day_diff < 31 && Math.ceil( day_diff / 7 ) + " hafta önce";
}

// Takes an ISO time and returns a string representing how
// long later the date represents.
function prettyReverseDate(time){
    var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")),
        diff = (( date.getTime() - (new Date()).getTime() ) / 1000),
        day_diff = Math.floor(diff / 86400);

    //console.log("Day dif : " + day_diff);

    if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
        return ;

    //put what ever string you wanted like "Expirs in # hour/minute/day"
    return day_diff == 0 && (
            diff < 60 && " şimdi" ||
            diff < 120 && " 1 dk" ||
            diff < 3600 && Math.floor( diff / 60 ) + " dakika" ||
            diff < 7200 && " 1 saat" ||
            diff < 86400 && Math.floor( diff / 3600 ) + " saat") ||
        day_diff == 1 && "1 gün" ||
        day_diff < 7 && day_diff + " gün" ||
        day_diff < 31 && Math.ceil( day_diff / 7 ) + " hafta";
}

// If jQuery is included in the page, adds a jQuery plugin to handle it as well
if ( typeof jQuery != "undefined" )
    jQuery.fn.prettyDate = function(){
        return this.each(function(){
            var date = prettyDate(this.title);
            if ( date )
                jQuery(this).text( date );
            else
                jQuery(this).text("n/a");
        });
    };

// If jQuery is included in the page, adds a jQuery plugin to handle it as well
if ( typeof jQuery != "undefined" )
    jQuery.fn.prettyReverseDate = function(){
        return this.each(function(){
            var date = prettyReverseDate(this.title);
            if ( date )
                jQuery(this).text( date );
            else
                //make parent element show expired
                jQuery(this).parent().text("süresi doldu");
        });
    };
4

2 回答 2

0

它是一个与时间格式相关的问题,prettyReverse 和漂亮的函数采用,并且它有以“。”开头的附加部分。删除该部分后,拆分功能问题消失了。

var date = new Date( ( ( time || "" ).replace( /-/g,"/" ).replace( /[TZ]/g," " ) ).split( '.' )[0] ),
于 2012-04-16T13:38:54.027 回答
0

首先使用 Firefox 的Firebug 插件来执行代码或显示执行过程中出现的错误消息。

当我只是复制并粘贴您的“prettyReverseDate”函数时,会出现语法错误。

使用 Firebug 调试您的代码,直到您看不到任何错误消息。然后检查实际的代码逻辑。

Firebug 文档: http: //getfirebug.com/wiki/index.php/Main_Page

于 2012-04-16T02:20:09.003 回答