15

I need some JS code that will take the created_at value from a Twitter feed and display it as xxxx ago.

I can find examples of creating the xxxx ago bit but not examples of getting the created_at bit into the correct format for JS.

Does anyone have an all in one function to do what I'm after?

example format Tue Apr 07 22:52:51 +0000 2009

Cannot use new Date(Date.parse("Tue Apr 07 22:52:51 +0000 2009")) as it gives an invalid date error in IE.

Scott
  • 3,967
  • 9
  • 38
  • 56
  • 2
    I'd recommend using DateJS to parse and format the dates: http://www.datejs.com/. – Chris Laplante Jul 01 '11 at 14:25
  • 3
    Try formatting like this: `new Date(Date.parse("Tue Apr 07 22:52:51 +0000 2009"))`. It returns a `Date` object, which you probably can pass to time ago libraries. – pimvdb Jul 01 '11 at 14:29
  • pimvdb probably meant Date.parse of DateJS library. I've tried on IE and seems to work – SelimOber Jul 02 '11 at 09:02

8 Answers8

47

Using moment.js without any plugin this is the custom format you need to use to parse the awkward Twitter date properly:

var tweetDate = 'Mon Dec 02 23:45:49 +0000 2013';
moment(tweetDate, 'dd MMM DD HH:mm:ss ZZ YYYY', 'en');
Thom4
  • 1,819
  • 17
  • 15
30

From the comments, and some code from the twitter widget here is the code I came up with:

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([^;]*)/)
    }
}();
Scott
  • 3,967
  • 9
  • 38
  • 56
5

Here's a french language translation of Brady's solution:

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 "à l'instant";}
    if (diff < 20) {return "il y a " + diff + " secondes";}
    if (diff < 40) {return "il y a une minute";}
    if (diff < 60) {return "il y a moins d'une minute";}
    if (diff <= 90) {return "il y a une minute";}
    if (diff <= 3540) {return "il y a " + Math.round(diff / 60) + " minutes";}
    if (diff <= 5400) {return "il y a 1 heure";}
    if (diff <= 86400) {return "il y a " + Math.round(diff / 3600) + " heures";}
    if (diff <= 129600) {return "il y a 1 jour";}
    if (diff < 604800) {return "il y a " + Math.round(diff / 86400) + " jours";}
    if (diff <= 777600) {return "il y a 1 semaine";}
    return system_date;
}

// from http://widgets.twimg.com/j/1/widget.js
var K = function () {
    var a = navigator.userAgent;
    return {
        ie: a.match(/MSIE\s([^;]*)/)
    }
}();
Termininja
  • 6,620
  • 12
  • 48
  • 49
myselfhimself
  • 559
  • 5
  • 12
3

You can use prettyDate by john Resig. It has also a JQuery plugin.

http://ejohn.org/blog/javascript-pretty-date/

SelimOber
  • 708
  • 6
  • 17
2
import * as luxon from 'luxon';

// https://stackoverflow.com/a/20478182/5932012
const TWITTER_DATE_FORMAT = 'EEE MMM d HH:mm:ss ZZZ yyyy';

export const parseTwitterDate = (dateStr: string): luxon.DateTime =>
    luxon.DateTime.fromString(dateStr, TWITTER_DATE_FORMAT);

export const formatTwitterDate = (dateTime: luxon.DateTime): string =>
    dateTime.toFormat(TWITTER_DATE_FORMAT);
Oliver Joseph Ash
  • 3,138
  • 2
  • 27
  • 47
0

I'm using jquery timeago:

var timeago = document.createElement('time')
var dt = new Date(Date.parse(data['status']['created_at']));
var datetimestr = '' + dt.getFullYear() + '-' + ("0" + (dt.getMonth() + 1)).slice(-2) + '-' + dt.getDate() + 'T' + dt.getHours() + ':' + dt.getMinutes() + ':' + dt.getSeconds() + 'Z'
$(timeago).addClass('timeago').attr('datetime', datetimestr).text(data['status']['created_at']);
if(jQuery().timeago) {$(timeago).timeago();}

More on timeago: http://timeago.yarp.com

toutpt
  • 5,145
  • 5
  • 38
  • 45
0

I was running into this issue myself, and I think it's time for a grown-up library. I use moment.js for time formatting, so I wrote an extension to do the right display parsing for Twitter:

https://github.com/hijonathan/moment.twitter

It also has a different method for returning super abbreviated timestamps (e.g. 7h) like in the Twitter iPhone app.

Jonathan K
  • 651
  • 6
  • 15
0

May be hella late but I just found out splitting the string by spaces and creating variables accordingly, easily allows you to breakdown the date.

// Ex. Thu Sep 28 03:40:33 +0000 2017

var tweetDate = data.tweet[i].created_at;

tweetDate = tweetDate.split(' ');

var tweetMo = tweetDate[1];