Skip to content

Commit 2c8c59b

Browse files
committed
Update timeAgo function to handle future times also
1 parent f2666f8 commit 2c8c59b

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

lib/utils.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,34 @@ export function timeAgo(isoDate: string): string {
1616
const diffInMonths = Math.floor(diffInDays / 30);
1717
const diffInYears = Math.floor(diffInDays / 365);
1818

19+
// Handle future dates
20+
if (diffInMs < 0) {
21+
const absDiffInYears = Math.abs(diffInYears);
22+
const absDiffInMonths = Math.abs(diffInMonths);
23+
const absDiffInDays = Math.abs(diffInDays);
24+
const absDiffInHours = Math.abs(diffInHours);
25+
const absDiffInMins = Math.abs(diffInMins);
26+
27+
if (absDiffInYears > 0) {
28+
return absDiffInYears === 1 ? "in 1 year" : `in ${absDiffInYears} years`;
29+
} else if (absDiffInMonths > 0) {
30+
return absDiffInMonths === 1
31+
? "in 1 month"
32+
: `in ${absDiffInMonths} months`;
33+
} else if (absDiffInDays > 0) {
34+
return absDiffInDays === 1 ? "tomorrow" : `in ${absDiffInDays} days`;
35+
} else if (absDiffInHours > 0) {
36+
return absDiffInHours === 1 ? "in 1 hour" : `in ${absDiffInHours} hours`;
37+
} else if (absDiffInMins > 0) {
38+
return absDiffInMins === 1
39+
? "in 1 minute"
40+
: `in ${absDiffInMins} minutes`;
41+
} else {
42+
return "just now";
43+
}
44+
}
45+
46+
// Handle past dates (existing logic)
1947
if (diffInYears > 0) {
2048
return diffInYears === 1 ? "1 year ago" : `${diffInYears} years ago`;
2149
} else if (diffInMonths > 0) {

0 commit comments

Comments
 (0)