date format plurals
This commit is contained in:
parent
c1b7d4210e
commit
6f3fc6daab
1 changed files with 9 additions and 4 deletions
|
|
@ -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();
|
||||
}
|
||||
Loading…
Reference in a new issue