简体   繁体   中英

human readable time javascript/jquery

I found this snippet at John Resig's blog:

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

My timezone on server is UTC , I am not sure what timezone will this code work on?

In my html i render my time as follows:

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

Will doing

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

humanize the time?

The above snippet does not take care of timezone. In case the server has UTC timezone, you will need to do an extra (d.getTimezoneOffset()*60000) to convert to local time.

The entire function below:

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

With a small modification, it should work: http://jsfiddle.net/gfPwa/

In the current plugin, the date string is being extracted using this.title which would not return anything for your <span> . In your case, we can instead extract the date string using $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 );
        });
    };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM