Return to Snippet

Revision: 5433
at March 9, 2008 00:51 by wbowers


Initial Code
function elapsedTime (createdAt)
{
    var ageInSeconds = (new Date().getTime() - new Date(createdAt).getTime()) / 1000;
    var s = function(n) { return n == 1 ? '' : 's' };
    if (ageInSeconds < 0) {
        return 'just now';
    }
    if (ageInSeconds < 60) {
        var n = ageInSeconds;
        return n + ' second' + s(n) + ' ago';
    }
    if (ageInSeconds < 60 * 60) {
        var n = Math.floor(ageInSeconds/60);
        return n + ' minute' + s(n) + ' ago';
    }
    if (ageInSeconds < 60 * 60 * 24) {
        var n = Math.floor(ageInSeconds/60/60);
        return n + ' hour' + s(n) + ' ago';
    }
    if (ageInSeconds < 60 * 60 * 24 * 7) {
        var n = Math.floor(ageInSeconds/60/60/24);
        return n + ' day' + s(n) + ' ago';
    }
    if (ageInSeconds < 60 * 60 * 24 * 31) {
        var n = Math.floor(ageInSeconds/60/60/24/7);
        return n + ' week' + s(n) + ' ago';
    }
    if (ageInSeconds < 60 * 60 * 24 * 365) {
        var n = Math.floor(ageInSeconds/60/60/24/31);
        return n + ' month' + s(n) + ' ago';
    }
    var n = Math.floor(ageInSeconds/60/60/24/365);
    return n + ' year' + s(n) + ' ago';
}

// Make date parseable in IE
function fixDate (d)
{
    var a = d.split(' ');
    var year = a.pop();
    return a.slice(0, 3).concat([year]).concat(a.slice(3)).join(' ');
}

Initial URL
http://jonaquino.blogspot.com/2006/12/twitter-increasing-number-of-twitters.html

Initial Description


Initial Title
Elapsed time string from time in seconds

Initial Tags
javascript, js, function, convert

Initial Language
JavaScript