Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

unix timestamp support #136

Closed
wants to merge 1 commit into from

3 participants

@gokercebeci

I added unix timestamp support:
Ex: 1372218564

@gokercebeci gokercebeci unix timestamp support
I added unix timestamp support:
Ex: <abbr class="timeago" title="1372218564">1372218564</abbr>
4a6d240
@rmm5t
Owner

Thanks for the effort on this, but this violates the HTML5 time tag. Timestamps should be in ISO8601 format. Same for the time microformat. Plus, this PR is missing tests.

@rmm5t rmm5t closed this
@gokercebeci

Yes, you are right. But, I can use the plugin with any tag.

<span class="timeago" title="1372218564">1372218564</span>

I'm working with foursquare api and it send me a timestamp. I mean, it can be more useful if it can handle timestamp.

@rmm5t
Owner

Sure, but timeago can handle Date objects too. If you're already looking at unix timestamps like this in Javascript, you can just assign the Date value to the element itself. No need to use the title attribute.

Here's a quick example. http://jsfiddle.net/Sq5gJ/

@gokercebeci

Thanks for explain but I think it's a little bit lame solution. I just wanted to use a plugin.
The below is a native example which can handle all of them, if I write JS code

http://jsfiddle.net/goker/3J9Dz/

(function timeAgo(selector) {

    var templates = {
        prefix: "",
        pastSuffix: " ago",
        featureSuffix: " later",
        seconds: "less than a minute",
        minute: "about a minute",
        minutes: "%d minutes",
        hour: "about an hour",
        hours: "about %d hours",
        day: "a day",
        days: "%d days",
        month: "about a month",
        months: "%d months",
        year: "about a year",
        years: "%d years"
    };
    var template = function(t, n) {
        return templates[t] && templates[t].replace(/%d/i, Math.abs(Math.round(n)));
    };

    var timer = function(time) {
        if (!time)
            return;
        time = time.replace(/\.\d+/, ""); // remove milliseconds
        time = time.replace(/-/, "/").replace(/-/, "/");
        time = time.replace(/T/, " ").replace(/Z/, " UTC");
        time = time.replace(/([\+\-]\d\d)\:?(\d\d)/, " $1$2"); // -04:00 -> -0400
        time = new Date(time * 1000 || time);

        var now = new Date();
        var seconds = ((now.getTime() - time) * .001) >> 0;
        var minutes = seconds / 60;
        var hours = minutes / 60;
        var days = hours / 24;
        var years = days / 365;

        return templates.prefix + (
                seconds < 45 && template('seconds', seconds) ||
                seconds < 90 && template('minute', 1) ||
                minutes < 45 && template('minutes', minutes) ||
                minutes < 90 && template('hour', 1) ||
                hours < 24 && template('hours', hours) ||
                hours < 42 && template('day', 1) ||
                days < 30 && template('days', days) ||
                days < 45 && template('month', 1) ||
                days < 365 && template('months', days / 30) ||
                years < 1.5 && template('year', 1) ||
                template('years', years)
                ) + templates.suffix;
    };

    var elements = document.getElementsByClassName('timeago');
    for (var i in elements) {
        var $this = elements[i];
        if (typeof $this === 'object') {
            $this.innerText = timer($this.getAttribute('title') || $this.getAttribute('datetime'));
        }
    }
    // update time every minute
    setTimeout(timeAgo, 60000);

})();

Thanks for interesting.
Regards.

@agdm

This helps my situation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 26, 2013
  1. @gokercebeci

    unix timestamp support

    gokercebeci authored
    I added unix timestamp support:
    Ex: <abbr class="timeago" title="1372218564">1372218564</abbr>
This page is out of date. Refresh to see the latest.
Showing with 1 addition and 1 deletion.
  1. +1 −1  jquery.timeago.js
View
2  jquery.timeago.js
@@ -107,7 +107,7 @@
s = s.replace(/-/,"/").replace(/-/,"/");
s = s.replace(/T/," ").replace(/Z/," UTC");
s = s.replace(/([\+\-]\d\d)\:?(\d\d)/," $1$2"); // -04:00 -> -0400
- return new Date(s);
+ return new Date(s * 1000 || s);
},
datetime: function(elem) {
var iso8601 = $t.isTime(elem) ? $(elem).attr("datetime") : $(elem).attr("title");
Something went wrong with that request. Please try again.