This repository has been archived on 2026-01-31. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
calendar-demo/client/src/utils.js
2021-01-02 13:11:59 +01:00

45 lines
1.4 KiB
JavaScript

function timeDifference(current, previous) {
const milliSecondsPerMinute = 60 * 1000;
const milliSecondsPerHour = milliSecondsPerMinute * 60;
const milliSecondsPerDay = milliSecondsPerHour * 24;
const milliSecondsPerMonth = milliSecondsPerDay * 30;
const milliSecondsPerYear = milliSecondsPerDay * 365;
const elapsed = current - previous;
if (elapsed < milliSecondsPerMinute / 3) {
return 'just now';
}
if (elapsed < milliSecondsPerMinute) {
return 'less than 1 min ago';
} else if (elapsed < milliSecondsPerHour) {
return (
Math.round(elapsed / milliSecondsPerMinute) +
' min ago'
);
} else if (elapsed < milliSecondsPerDay) {
return (
Math.round(elapsed / milliSecondsPerHour) + ' h ago'
);
} else if (elapsed < milliSecondsPerMonth) {
return (
Math.round(elapsed / milliSecondsPerDay) + ' days ago'
);
} else if (elapsed < milliSecondsPerYear) {
return (
Math.round(elapsed / milliSecondsPerMonth) + ' mo ago'
);
} else {
return (
Math.round(elapsed / milliSecondsPerYear) +
' years ago'
);
}
}
export function timeDifferenceForDate(date) {
const now = new Date().getTime();
const updated = new Date(date).getTime();
return timeDifference(now, updated);
}