20 lines
604 B
JavaScript
20 lines
604 B
JavaScript
|
|
export function ago(date) {
|
||
|
|
const now = new Date();
|
||
|
|
const diff = now - date;
|
||
|
|
if (diff < 1000) {
|
||
|
|
return "just now";
|
||
|
|
}
|
||
|
|
if (diff < 60 * 1000) {
|
||
|
|
return Math.floor(diff / 1000) + " seconds ago";
|
||
|
|
}
|
||
|
|
if (diff < 60 * 60 * 1000) {
|
||
|
|
return Math.floor(diff / (60 * 1000)) + " minutes ago";
|
||
|
|
}
|
||
|
|
if (diff < 24 * 60 * 60 * 1000) {
|
||
|
|
return Math.floor(diff / (60 * 60 * 1000)) + " hours ago";
|
||
|
|
}
|
||
|
|
if (diff < 7 * 24 * 60 * 60 * 1000) {
|
||
|
|
return Math.floor(diff / (24 * 60 * 60 * 1000)) + " days ago";
|
||
|
|
}
|
||
|
|
return date.toLocaleDateString();
|
||
|
|
}
|