fix: improve relative time display

This commit is contained in:
Julian Tölle 2023-02-19 17:53:25 +01:00
parent d6a70f5436
commit d8f4a427f0

View file

@ -16,13 +16,24 @@
try { try {
date = new Date(element.innerText); date = new Date(element.innerText);
} catch (err) { } catch (err) {
console.error("Element does have a proper date"); console.error("Element does not have a proper date");
return; return;
} }
const diffMs = date - new Date(); const diffMs = date - new Date();
const daysSince = diffMs / 1000 / 60 / 60 / 24; const hoursSince = diffMs / 1000 / 60 / 60;
const formatted = formatter.format(daysSince, "days"); const daysSince = hoursSince / 24;
let formatted = "";
if (daysSince > 1) {
formatted = formatter.format(daysSince, "days");
} else if (hoursSince > 1) {
formatted = formatter.format(daysSince, "hours");
} else {
formatted = "Just Now!";
}
const capitalized = const capitalized =
formatted.charAt(0).toUpperCase() + formatted.slice(1); formatted.charAt(0).toUpperCase() + formatted.slice(1);