date format plurals

This commit is contained in:
jableader 2024-10-15 19:46:17 +11:00
parent c1b7d4210e
commit 6f3fc6daab

View file

@ -1,3 +1,8 @@
function plural(num, unit) {
num = Math.floor(num);
return num + " " + unit + (num === 1 ? "" : "s");
}
export function ago(date) {
const now = new Date();
const diff = now - date;
@ -5,16 +10,16 @@ export function ago(date) {
return "just now";
}
if (diff < 60 * 1000) {
return Math.floor(diff / 1000) + " seconds ago";
return plural(diff / 1000, "second") + " ago";
}
if (diff < 60 * 60 * 1000) {
return Math.floor(diff / (60 * 1000)) + " minutes ago";
return plural(diff / (60 * 1000), "minute") + " ago";
}
if (diff < 24 * 60 * 60 * 1000) {
return Math.floor(diff / (60 * 60 * 1000)) + " hours ago";
return plural(diff / (60 * 60 * 1000), "hour") + " ago";
}
if (diff < 7 * 24 * 60 * 60 * 1000) {
return Math.floor(diff / (24 * 60 * 60 * 1000)) + " days ago";
return plural(diff / (24 * 60 * 60 * 1000), "day") + " ago";
}
return date.toLocaleDateString();
}