mirror of https://github.com/Lissy93/dashy.git
🔀 Merge pull request #1217 from altearius/FEATURE/temporal-plurality
🩹 Singular/plural forms for time counts.
This commit is contained in:
commit
d718905779
|
@ -141,12 +141,27 @@ export const getTimeDifference = (startTime, endTime) => {
|
||||||
const msDifference = new Date(endTime).getTime() - new Date(startTime).getTime();
|
const msDifference = new Date(endTime).getTime() - new Date(startTime).getTime();
|
||||||
const diff = Math.abs(Math.round(msDifference / 1000));
|
const diff = Math.abs(Math.round(msDifference / 1000));
|
||||||
const divide = (time, round) => Math.round(time / round);
|
const divide = (time, round) => Math.round(time / round);
|
||||||
if (diff < 60) return `${divide(diff, 1)} seconds`;
|
|
||||||
if (diff < 3600) return `${divide(diff, 60)} minutes`;
|
const periods = [
|
||||||
if (diff < 86400) return `${divide(diff, 3600)} hours`;
|
{ noun: 'second', value: 1 },
|
||||||
if (diff < 604800) return `${divide(diff, 86400)} days`;
|
{ noun: 'minute', value: 60 },
|
||||||
if (diff < 31557600) return `${divide(diff, 604800)} weeks`;
|
{ noun: 'hour', value: 3600 },
|
||||||
if (diff >= 31557600) return `${divide(diff, 31557600)} years`;
|
{ noun: 'day', value: 86400 },
|
||||||
|
{ noun: 'week', value: 604800 },
|
||||||
|
{ noun: 'fortnight', value: 1209600 },
|
||||||
|
{ noun: 'month', value: 2628000 },
|
||||||
|
{ noun: 'year', value: 31557600 },
|
||||||
|
];
|
||||||
|
|
||||||
|
for (let idx = 0; idx < periods.length; idx += 1) {
|
||||||
|
if (diff < (periods[idx + 1]?.value ?? Infinity)) {
|
||||||
|
const period = periods[idx];
|
||||||
|
const value = divide(diff, period.value);
|
||||||
|
const noun = value === 1 ? period.noun : `${period.noun}s`;
|
||||||
|
return `${value} ${noun}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return 'unknown';
|
return 'unknown';
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue