Skip to content

Instantly share code, notes, and snippets.

@codeincontext
Last active January 25, 2023 17:05
Show Gist options
  • Save codeincontext/1285806 to your computer and use it in GitHub Desktop.
Save codeincontext/1285806 to your computer and use it in GitHub Desktop.

Revisions

  1. Adam Howard revised this gist Feb 23, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -11,8 +11,8 @@ function timeAgo(time){
    var diff = (new Date() - new Date(time*1000)) / 1000;
    if (diff < 5) return "now";

    var i = 0;
    while (var unit = units[i++]) {
    var i = 0, unit;
    while (unit = units[i++]) {
    if (diff < unit.limit || !unit.limit){
    var diff = Math.floor(diff / unit.in_seconds);
    return diff + " " + unit.name + (diff>1 ? "s" : "");
  2. Adam Howard revised this gist Feb 23, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@ function timeAgo(time){
    if (diff < 5) return "now";

    var i = 0;
    while (unit = units[i++]) {
    while (var unit = units[i++]) {
    if (diff < unit.limit || !unit.limit){
    var diff = Math.floor(diff / unit.in_seconds);
    return diff + " " + unit.name + (diff>1 ? "s" : "");
  3. skattyadz created this gist Oct 13, 2011.
    21 changes: 21 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    function timeAgo(time){
    var units = [
    { name: "second", limit: 60, in_seconds: 1 },
    { name: "minute", limit: 3600, in_seconds: 60 },
    { name: "hour", limit: 86400, in_seconds: 3600 },
    { name: "day", limit: 604800, in_seconds: 86400 },
    { name: "week", limit: 2629743, in_seconds: 604800 },
    { name: "month", limit: 31556926, in_seconds: 2629743 },
    { name: "year", limit: null, in_seconds: 31556926 }
    ];
    var diff = (new Date() - new Date(time*1000)) / 1000;
    if (diff < 5) return "now";

    var i = 0;
    while (unit = units[i++]) {
    if (diff < unit.limit || !unit.limit){
    var diff = Math.floor(diff / unit.in_seconds);
    return diff + " " + unit.name + (diff>1 ? "s" : "");
    }
    };
    }