allons-y
Version:
Yet another JS/TS tools
11 lines (10 loc) • 746 B
JavaScript
export const intervalReadable = (sec, short = false) => [
[Math.floor(sec / 31536000), short ? 'y' : 'years'],
[Math.floor((sec % 31536000) / 86400), short ? 'd' : 'days'],
[Math.floor(((sec % 31536000) % 86400) / 3600), short ? 'h' : 'hours'],
[Math.floor((((sec % 31536000) % 86400) % 3600) / 60), short ? 'm' : 'minutes'],
[Math.round((((sec % 31536000) % 86400) % 3600) % 60), short ? 's' : 'seconds'],
]
.reduce((value, level) => value.concat(level[0] === 0 ? [] : [`${level[0]}${short ? '' : ' '}${level[1]}`]), [])
.join(' ') || (short ? '< 1s' : 'less than a second');
export const totalTimeReadable = (startDate, short = false) => intervalReadable((new Date().getTime() - startDate.getTime()) / 1000, short);