# Posted By

wbowers on 03/09/08

# Statistics

Viewed 221 times
Favorited by 0 user(s)

# Elapsed time string from time in seconds

/ Published in: JavaScript
`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 IEfunction fixDate (d){    var a = d.split(' ');    var year = a.pop();    return a.slice(0, 3).concat([year]).concat(a.slice(3)).join(' ');}`