munch-ease-frontend/src/dateformats.js

20 lines
604 B
JavaScript
Raw Normal View History

2024-10-15 08:40:06 +00:00
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();
}