1

我在 John Resig 的博客上找到了这个片段:

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 && "just now" ||
            diff < 120 && "1 min" ||
            diff < 3600 && Math.floor( diff / 60 ) + " mins" ||
            diff < 7200 && "1 hour" ||
            diff < 86400 && Math.floor( diff / 3600 ) + " hours") ||
        day_diff == 1 && "Yesterday" ||
        day_diff < 7 && day_diff + " d" ||
        day_diff < 31 && Math.ceil( day_diff / 7 ) + " w";
}

// 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 );
        });
    };

我在服务器上的时区是UTC,我不确定这段代码将在哪个时区工作?

在我的 html 中,我将时间渲染如下:

<span id="p-date">2012-09-26T00:12:15</span>

会做

  $(function() {
  $("#p-date").prettyDate();
  setInterval(function(){ $("#p-date").prettyDate(); }, 5000);
  });

时间人性化?

4

2 回答 2

3

上面的代码片段不考虑时区。如果服务器有 UTC 时区,您需要做一个额外(d.getTimezoneOffset()*60000)的转换来转换为本地时间。

整个函数如下:

function prettyDate(time){
    d = new Date();
    var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")),
        diff = ((d.getTime() + (d.getTimezoneOffset()*60000) - 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 && "just now" ||
            diff < 120 && "1 min ago" ||
            diff < 3600 && Math.floor( diff / 60 ) + " mins ago" ||
            diff < 7200 && "1 hour ago" ||
            diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
        day_diff == 1 && "Yesterday" ||
        day_diff < 7 && day_diff + " days ago" ||
        day_diff < 31 && Math.ceil( day_diff / 7 ) + " week ago";
}

// 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 $this = jQuery(this),  
                date = prettyDate($this.text());
            if ( date )
                $this.text( date );
        });
    };

$(function() {
    $(".p-date").prettyDate();
    setInterval(function(){ $(".p-date").prettyDate(); }, 5000);
});
于 2012-09-27T09:46:59.883 回答
1

稍作修改,它应该可以工作:http: //jsfiddle.net/gfPwa/

在当前插件中,日期字符串正在被提取,this.title它不会为您的<span>. 在您的情况下,我们可以改为使用$this.text().

if ( typeof jQuery != "undefined" )
    jQuery.fn.prettyDate = function(){
        return this.each(function(){
            var $this = jQuery(this),   // cache jQuery(this)
                date = prettyDate($this.text());  // get date string from .text()
            if ( date )
                $this.text( date );
        });
    };
于 2012-09-26T12:59:20.410 回答