-
-
Save kosso/986098 to your computer and use it in GitHub Desktop.
calculate # of ms/seconds/minutes/hours/days in the past
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Copyright (c) 2011 Jed Schmidt, http://jed.is | |
Permission is hereby granted, free of charge, to any person obtaining | |
a copy of this software and associated documentation files (the | |
"Software"), to deal in the Software without restriction, including | |
without limitation the rights to use, copy, modify, merge, publish, | |
distribute, sublicense, and/or sell copies of the Software, and to | |
permit persons to whom the Software is furnished to do so, subject to | |
the following conditions: | |
The above copyright notice and this permission notice shall be | |
included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "timeAgo", | |
"keywords": ["time", "chronological", "ago"] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment