mirror of
https://github.com/qwerinope/qweribot.git
synced 2025-12-19 05:51:37 +01:00
18 lines
743 B
TypeScript
18 lines
743 B
TypeScript
export function buildTimeString(time1: number, time2: number) {
|
|
const diff = Math.abs(time1 - time2);
|
|
const timeobj = {
|
|
day: Math.floor(diff / (1000 * 60 * 60 * 24)),
|
|
hour: Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)),
|
|
minute: Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60)),
|
|
second: Math.floor((diff % (1000 * 60)) / 1000)
|
|
};
|
|
const stringarray: string[] = [];
|
|
for (const [unit, value] of Object.entries(timeobj)) {
|
|
if (value === 0) continue;
|
|
if (unit === 'second' && timeobj.day > 0) continue;
|
|
stringarray.push(`${value} ${unit}${value === 1 ? '' : 's'}`);
|
|
};
|
|
const last = stringarray.pop();
|
|
return stringarray.length === 0 ? last : stringarray.join(', ') + " and " + last;
|
|
};
|