hekevintran
hekevintran

Reputation: 23722

Facebook timestamps and JavaScript

On Facebook the timestamps update themselves. If you post something, the timestamp will say, "a few seconds ago". If you keep the page open an wait a minute it will change to say, "one minute ago".

I suppose that the time objects on the page are rendering themselves every few seconds. Is there a JavaScript library that can do computations on datetime objects and produce these strings? In Python there is the datetime module and python-dateutil. Because this is client side code, how do you address templating and i18n?

Upvotes: 1

Views: 2172

Answers (3)

deceze
deceze

Reputation: 522081

timeago, a jQuery plugin

Upvotes: 6

Rafid
Rafid

Reputation: 20189

Why would you need a library for that? It shouldn't be that complicated. Here is an example:

function TimeToString(var seconds) {
    if (seconds < 60)
        return seconds + " seconds ago.";
    else if (seconds < 3600) {
        var minutes = Math.floor(seconds / 60);
        return minutes + minutes ago.";
    }
    ...
    ...
}

Now you only need to find the difference in time and post it to this function. You can then make a timer to make objects update themselves automatically every 1 minute, for example.

Please let me know if you need more clarification.

UPDATE: There seems to be a similar question here:

https://stackoverflow.com/questions/48726/best-javascript-i18n-techniques-ajax-dates-times-numbers-currency

Upvotes: 0

vsr
vsr

Reputation: 3198

Have a look at John Resig's prettyDate . It doesn't handle i18n though; but you can extend it.

Upvotes: 0

Related Questions