首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >人类可读时间javascript/jquery

人类可读时间javascript/jquery
EN

Stack Overflow用户
提问于 2012-09-26 12:39:26
回答 2查看 2.3K关注 0票数 1

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

代码语言:javascript
复制
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中,我将我的时间呈现如下:

代码语言:javascript
复制
<span id="p-date">2012-09-26T00:12:15</span>

威尔做

代码语言:javascript
复制
  $(function() {
  $("#p-date").prettyDate();
  setInterval(function(){ $("#p-date").prettyDate(); }, 5000);
  });

人性化的时间?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-09-27 09:46:59

上面的片段不负责时区。如果服务器有UTC时区,则需要执行额外的(d.getTimezoneOffset()*60000)转换为本地时间。

整个职能如下:

代码语言:javascript
复制
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);
});
票数 3
EN

Stack Overflow用户

发布于 2012-09-26 12:59:20

只要稍加修改,它就能工作:http://jsfiddle.net/gfPwa/

在当前插件中,使用this.title提取日期字符串,它不会为<span>返回任何内容。在您的示例中,我们可以使用$this.text()提取日期字符串。

代码语言:javascript
复制
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 );
        });
    };
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12601921

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档