mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-06 02:00:46 +00:00
fix "-1 seconds ago" bug
This commit is contained in:
1 parent
4ec6949900
commit
f1eefb8add
1 file changed
+23
-18
@@ -9,33 +9,38 @@ function useTimeAgo(timestamp: string) {
|
||||
const then = new Date(timestamp);
|
||||
|
||||
const seconds = Math.floor((now.getTime() - then.getTime()) / 1000);
|
||||
const minutes = Math.floor(seconds / 60);
|
||||
const hours = Math.floor(minutes / 60);
|
||||
const days = Math.floor(hours / 24);
|
||||
const months = Math.floor(days / 30);
|
||||
const years = Math.floor(months / 12);
|
||||
|
||||
if (years > 0) {
|
||||
setTimeAgo(`${years} ${years === 1 ? "year" : "years"} ago`);
|
||||
} else if (months > 0) {
|
||||
setTimeAgo(`${months} ${months === 1 ? "month" : "months"} ago`);
|
||||
} else if (days > 0) {
|
||||
setTimeAgo(`${days} ${days === 1 ? "day" : "days"} ago`);
|
||||
} else if (hours > 0) {
|
||||
setTimeAgo(`${hours} ${hours === 1 ? "hour" : "hours"} ago`);
|
||||
} else if (minutes > 0) {
|
||||
setTimeAgo(`${minutes} ${minutes === 1 ? "minute" : "minutes"} ago`);
|
||||
if (seconds < 10) {
|
||||
setTimeAgo("a few seconds ago");
|
||||
} else if (seconds < 60) {
|
||||
setTimeAgo(`${seconds} seconds ago`);
|
||||
} else {
|
||||
setTimeAgo(`${seconds} ${seconds === 1 ? "second" : "seconds"} ago`);
|
||||
const minutes = Math.floor(seconds / 60);
|
||||
const hours = Math.floor(minutes / 60);
|
||||
const days = Math.floor(hours / 24);
|
||||
const months = Math.floor(days / 30);
|
||||
const years = Math.floor(months / 12);
|
||||
|
||||
if (years > 0) {
|
||||
setTimeAgo(`${years} ${years === 1 ? "year" : "years"} ago`);
|
||||
} else if (months > 0) {
|
||||
setTimeAgo(`${months} ${months === 1 ? "month" : "months"} ago`);
|
||||
} else if (days > 0) {
|
||||
setTimeAgo(`${days} ${days === 1 ? "day" : "days"} ago`);
|
||||
} else if (hours > 0) {
|
||||
setTimeAgo(`${hours} ${hours === 1 ? "hour" : "hours"} ago`);
|
||||
} else if (minutes > 0) {
|
||||
setTimeAgo(`${minutes} ${minutes === 1 ? "minute" : "minutes"} ago`);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
calculateTimeAgo();
|
||||
|
||||
// Update the time difference every minute
|
||||
const intervalId = setInterval(calculateTimeAgo, 60000);
|
||||
// const intervalId = setInterval(calculateTimeAgo, 60000);
|
||||
|
||||
return () => clearInterval(intervalId);
|
||||
// return () => clearInterval(intervalId);
|
||||
}, [timestamp]);
|
||||
|
||||
return timeAgo;
|
||||
|
||||
Reference in new issue
Block a user