Java script code to parse twitter created at value

After a long search in the net i found how to convert twitter create_at time stamp data into friendly time format. Using the below javascript code you can pass the create_at data format which you get from twitter search json. Twitter create_at time format looks link this (Thu, 03 Jan 2013 20:18:17 +0000), which contains all the date informations.

Check the below code to convert twitter created at value into friendly time format.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// from http://widgets.twimg.com/j/1/widget.js
var K = function () {
    var a = navigator.userAgent;
    return {
        ie: a.match(/MSIEs([^;]*)/)
    }
}();
 
var twitterDate = function (a) {
    var b = new Date();
    var c = new Date(a);
    if (K.ie) {
        c = Date.parse(a.replace(/( +)/, ' UTC$1'))
    }
    var d = b - c;
    var e = 1000,
        minute = e * 60,
        hour = minute * 60,
        day = hour * 24,
        week = day * 7;
    if (isNaN(d) || d < 0) {
        return ""
    }
    if (d < e * 7) { return "right now"; }
    if (d < minute) { return Math.floor(d / e) + " seconds ago"; }
    if (d < minute * 2) { return "about 1 minute ago"; }
    if (d < hour) { return Math.floor(d / minute) + " minutes ago"; }
    if (d < hour * 2) { return "about 1 hour ago"; }
    if (d < day) { return Math.floor(d / hour) + " hours ago"; }
    if (d > day && d < day * 2) { return "yesterday"; }
    if (d < day * 365) { return Math.floor(d / day) + " days ago"; )
    } else { return "over a year ago"; }
};
console.log(twitterDate("Thu, 03 Jan 2013 20:18:17 +0000"));