Created
September 22, 2016 12:51
-
-
Save ergoithz/5da2d4bc24cb5f28d9c19173774e76fd to your computer and use it in GitHub Desktop.
Fuzzy time from seconds.
This file contains hidden or 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
intervals = [ | |
{t: 0.001, s: 'a millisecond', p: '%d milliseconds'}, | |
{t: 1, s: 'a second', p: '%d seconds'}, | |
{t: 60, s: 'a minute', p: '%d minutes', m: 1}, | |
{t: 3600, s: 'an hour', p: '%d hours', m: 1}, | |
{t: 86400, s: 'a day', p: '%d days', m: 1}, | |
{t: 604800, s: 'a week', p: '%d weeks', m: 1}, | |
{t: 2629800, s: 'a month', p: '%d months', m: 2}, | |
{t: 31587840, s: 'a year', p: '%d years', m: 2}, | |
]; | |
function humanize(seconds, precise){ | |
const | |
range = {start: 0, end: 0}; | |
parts = []; | |
for (let i = 1, l = intervals.length; i < l && seconds / intervals[i].t >= 1; i++) { | |
range.end = i; | |
} | |
if(!precise){ | |
range.start = range.end - (intervals[range.end].m || 0); | |
} | |
for (let i = range.end, l = range.start - 1; i > l && seconds; i--) { | |
const | |
interval = intervals[i], | |
part = parseInt(seconds / interval.t); | |
if(part > 0){ | |
seconds = Math.max(0, seconds - part * interval.t); | |
parts.push(part == 1 ? interval.s.replace('%d', part) : interval.p.replace('%d', part)); | |
} | |
} | |
return parts[1] ? [parts.pop(), parts.join(', ')].reverse().join(' and ') : parts[0]; | |
} | |
console.log('Fuzzy:', humanize(1000000000)); | |
console.log('Precise:', humanize(1000000000, true)); | |
console.log('Fuzzy:' ,humanize(10000000)); | |
console.log('Precise:', humanize(10000000, true)); | |
console.log('Fuzzy:', humanize(1000000)); | |
console.log('Precise:', humanize(1000000, true)); | |
console.log('Fuzzy:', humanize(100000)); | |
console.log('Precise:', humanize(100000, true)); | |
console.log('Fuzzy:', humanize(10000)); | |
console.log('Precise:', humanize(10000, true)); | |
console.log('Fuzzy:', humanize(1000)); | |
console.log('Precise:', humanize(1000, true)); | |
console.log('Fuzzy:', humanize(100.25)); | |
console.log('Precise:', humanize(100.25, true)); | |
console.log('Fuzzy:', humanize(10.25)); | |
console.log('Precise:', humanize(10.25, true)); | |
console.log('Fuzzy:', humanize(0.25)); | |
console.log('Precise:', humanize(0.25, true)); | |
/* | |
Fuzzy: 31 years, 7 months and 3 weeks | |
Precise: 31 years, 7 months, 3 weeks, 6 days, 9 hours, 52 minutes and 40 seconds | |
Fuzzy: 3 months, 3 weeks and 3 days | |
Precise: 3 months, 3 weeks, 3 days, 10 hours, 16 minutes and 40 seconds | |
Fuzzy: a week and 4 days | |
Precise: a week, 4 days, 13 hours, 46 minutes and 40 seconds | |
Fuzzy: a day and 3 hours | |
Precise: a day, 3 hours, 46 minutes and 40 seconds | |
Fuzzy: 2 hours and 46 minutes | |
Precise: 2 hours, 46 minutes and 40 seconds | |
Fuzzy: 16 minutes and 40 seconds | |
Precise: 16 minutes and 40 seconds | |
Fuzzy: a minute and 40 seconds | |
Precise: a minute, 40 seconds and 250 milliseconds | |
Fuzzy: 10 seconds | |
Precise: 10 seconds and 250 milliseconds | |
Fuzzy: 250 milliseconds | |
Precise: 250 milliseconds | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment