Ensure ordering
This commit is contained in:
@@ -9,14 +9,15 @@ import i18n from "i18next";
|
||||
let _relativeDateFormat: Intl.RelativeTimeFormat | undefined;
|
||||
|
||||
export const formatDateRelative = (date: Date) => {
|
||||
const units = {
|
||||
year: 24 * 60 * 60 * 1000 * 365,
|
||||
month: (24 * 60 * 60 * 1000 * 365) / 12,
|
||||
day: 24 * 60 * 60 * 1000,
|
||||
hour: 60 * 60 * 1000,
|
||||
minute: 60 * 1000,
|
||||
second: 1000,
|
||||
};
|
||||
const units: [Intl.RelativeTimeFormatUnit, number][] = [
|
||||
["year", 24 * 60 * 60 * 1000 * 365],
|
||||
["month", (24 * 60 * 60 * 1000 * 365) / 12],
|
||||
["day", 24 * 60 * 60 * 1000],
|
||||
["hour", 60 * 60 * 1000],
|
||||
["minute", 60 * 1000],
|
||||
["second", 1000],
|
||||
];
|
||||
|
||||
const relativeDateFormat = (_relativeDateFormat ??=
|
||||
new Intl.RelativeTimeFormat(i18n.language, {
|
||||
localeMatcher: "best fit",
|
||||
@@ -24,13 +25,13 @@ export const formatDateRelative = (date: Date) => {
|
||||
style: "short",
|
||||
}));
|
||||
|
||||
// "Math.abs" accounts for both past and future scenarios.
|
||||
// Math.abs accounts for both past and future scenarios.
|
||||
const elapsed = Math.abs(date.getTime() - Date.now());
|
||||
|
||||
for (const u in units)
|
||||
if (elapsed > units[u] || u === "second")
|
||||
return relativeDateFormat.format(
|
||||
Math.round(elapsed / units[u]),
|
||||
u as Intl.RelativeTimeFormatUnit,
|
||||
);
|
||||
for (const [u, d] of units) {
|
||||
if (elapsed > d)
|
||||
return relativeDateFormat.format(Math.round(elapsed / d), u);
|
||||
}
|
||||
|
||||
return relativeDateFormat.format(Math.round(elapsed / 1000), "second");
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user