Skip to content

Instantly share code, notes, and snippets.

@jed
Forked from 140bytes/LICENSE.txt
Created May 10, 2011 23:27
Show Gist options
  • Star 49 You must be signed in to star a gist
  • Fork 15 You must be signed in to fork a gist
  • Save jed/965606 to your computer and use it in GitHub Desktop.
Save jed/965606 to your computer and use it in GitHub Desktop.
calculate # of ms/seconds/minutes/hours/days in the past
function(
a, // a Date object
b, // placeholder
c // placeholder
){
for ( // whittle the date down to biggest unit
b = [ // an array containing the
1e3, // ms in a second,
60, // seconds in a minute,
60, // minutes in an hour, and
24 // hours in a day.
],
a = new Date - a, // get ms since date passed.
c = 0; // initialize the cursor.
a > 2 * b[c]; // while the 2 x time left exceeds the current unit,
a /= b[c++] // divide the time by the unit and increment.
); // (note: the 2* above ensures the units are plural!)
return ~~a + // the rounded remaining unit +
" " + // a space + the unit name, which we get by
"m0second0minute0hour0day" // taking a 0-delimited string of names
.split(0) // splitting it by 0, and
[c] + // taking the final cursor, +
"s ago" // the plural and "ago"
}
function(a,b,c){for(b=[1e3,60,60,24],a=new Date-a,c=0;a>2*b[c];a/=b[c++]);return~~a+" "+"m0second0minute0hour0day".split(0)[c]+"s ago"}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Jed Schmidt <http://jed.is>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "timeAgo",
"keywords": ["time", "chronological", "ago"]
}
@tsenart
Copy link

tsenart commented May 16, 2011

Very well done! One question though... is it supposed to show 60 seconds ago instead of 1 minute ago?

@jed
Copy link
Author

jed commented May 16, 2011

this is a compromise for space. it will show seconds up to 119, and then 2 minutes. this avoids renderings that are incorrect, like "1 minutes ago", and awkward, like "1 minute(s) ago". 135 bytes is too short to handle singular/plural...

@tiptronic
Copy link

Hi Jed,

I'd prefer adding the singular/plurar, which adds just a couple of bytes, but from an aesthetic pov it seems worth it? No? (This also fixes the 60seconds vs. 1minute thing as mentioned by @tsenart)

andy

function(
a, // a Date object
b, // placeholder
c // placeholder
){
for ( // whittle the date down to biggest unit
b = [ // an array containing the
1e3, // ms in a second,
60, // seconds in a minute,
60, // minutes in an hour, and
24 // hours in a day.
],
a = new Date - a, // get ms since date passed.
c = 0; // initialize the cursor.
a >= b[c]; // while the time left exceeds the current unit,
a /= b[c++] // divide the time by the unit and increment.
);
return ~ a + // the rounded remaining unit + // <- had to put a space between the two tildes to avoid display-problems in this text field
" " + // a space + the unit name, which we get by
"m0second0minute0hour0day" // taking a 0-delimited string of names
.split(0) // splitting it by 0, and
[c] + // taking the final cursor, +
(
~a > 1 ? "s ": "") + //adding singular/plurar is worth the couple of bytes
" ago" // and "ago"
}

//Please note I had to put a space between the rounding operators, since they triggered a strike-through

@tiptronic
Copy link

OK - see a slight flaw, if the time < 2 ms, but I could live with that ;-)

@softprops
Copy link

awesome! this is probably one of my favorite 140bytes.

@zoloadang
Copy link

well done!

@adjenks
Copy link

adjenks commented Aug 22, 2019

How about Years?
Aeons? Ha...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment