1

Here is an example of one of my tweet objects:

date: "2016-01-12T10:13:50Z"
formatted_date: "January 12, 2016 - 10:13 AM"
formatted_date_difference: "-20798 sec ago"
id: "68358314540"
link: true
literal_text: "One more mention of this: graphic novel night with DEATH theme. About 20 tickets remain. Come!... https://t.co/jInCQ2c8hv"
start_epoch: 1452615230
text: "One more mention of this: graphic novel night with DEATH theme. About 20 tickets remain. Come!... https://t.co/jInCQ2c8hv"
user_name: "watsoncomedian"

I'm trying to work with this formatted_date_difference: "-20798 sec ago"

So far I found this function from this question.

function beautifyTime(timeAgo) {
    var seconds = Math.floor((new Date() - timeAgo) / 1000),
    intervals = [
        Math.floor(seconds / 31536000),
        Math.floor(seconds / 2592000),
        Math.floor(seconds / 86400),
        Math.floor(seconds / 3600),
        Math.floor(seconds / 60)
    ],
    times = [
        'year',
        'month',
        'day',
        'hour',
        'minute'
    ];

    var key;
    for(key in intervals) {
        if (intervals[key] > 1)  
            return intervals[key] + ' ' + times[key] + 's ago';
        else if (intervals[key] === 1) 
            return intervals[key] + ' ' + times[key] + ' ago';
    }

    return Math.floor(seconds) + ' seconds ago';
}

However it always returns 49 years ago when I enter in beautifyTime(16275); or beautifyTime(20798) as a test.

Community
  • 1
  • 1
Leon Gaban
  • 36,509
  • 115
  • 332
  • 529
  • If you have a string saying `"-20798 sec ago"`, why do you need to put that through a function that should return `"20798 seconds ago"`, it seems like just a little string replacement would do that ? – adeneo Jan 12 '16 at 17:49

2 Answers2

0

Try this one, it was made for it: moment

gog
  • 11,788
  • 23
  • 67
  • 129
  • Thanks, that works :) I'm also going to post the factory I created from another answer that also worked for me – Leon Gaban Jan 12 '16 at 17:57
  • would you mind a look at this question? http://stackoverflow.com/questions/35093347/moment-js-amdateformat-always-returning-date-from-1970 :) – Leon Gaban Jan 29 '16 at 20:21
0

Here is an Angular service I created from the answer here: JavaScript code to display Twitter created_at as xxxx ago

(function() {
    angular
        .module('tweetDateFactory', [])
        .factory('TweetDateFactory', factory);

    factory.$inject = [];

    function factory() {

        /** Init TweetDateFactory scope */
        /** ----------------------------------------------------------------- */
        var tweetDateFactory = {
            parseTwitterDate : parseTwitterDate
        }

        return tweetDateFactory;
        ////////////////////////////////////////////////////////////////////////

        function parseTwitterDate(tdate) {
            var system_date = new Date(Date.parse(tdate));
            var user_date = new Date();
            if (K.ie) {
                system_date = Date.parse(tdate.replace(/( \+)/, ' UTC$1'))
            }
            var diff = Math.floor((user_date - system_date) / 1000);
            if (diff <= 1) {return "just now";}
            if (diff < 20) {return diff + " seconds ago";}
            if (diff < 40) {return "half a minute ago";}
            if (diff < 60) {return "less than a minute ago";}
            if (diff <= 90) {return "one minute ago";}
            if (diff <= 3540) {return Math.round(diff / 60) + " minutes ago";}
            if (diff <= 5400) {return "1 hour ago";}
            if (diff <= 86400) {return Math.round(diff / 3600) + " hours ago";}
            if (diff <= 129600) {return "1 day ago";}
            if (diff < 604800) {return Math.round(diff / 86400) + " days ago";}
            if (diff <= 777600) {return "1 week ago";}
            return "on " + system_date;
        }

        // from http://widgets.twimg.com/j/1/widget.js
        var K = function () {
            var a = navigator.userAgent;
            return {
                ie: a.match(/MSIE\s([^;]*)/)
            }
        }();
    }
})();
Community
  • 1
  • 1
Leon Gaban
  • 36,509
  • 115
  • 332
  • 529