Skip to content

Commit d2e0c90

Browse files
authored
fix: Add month logic to generateElapsedTimeText function
1 parent 2d3d5f4 commit d2e0c90

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/utils/commonUtils.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,24 @@ type FetchParams = {
2525
};
2626

2727
export const generateElapsedTimeText = (timeString: string) => {
28-
const currentTime = Date.now();
28+
const currentTime = new Date().getTime();
2929
const targetTime = new Date(timeString).getTime();
30-
const timeDiff = currentTime - targetTime;
30+
const millisecondsDiff = currentTime - targetTime;
3131

32-
const seconds = Math.floor(timeDiff / 1000);
32+
const seconds = Math.floor(millisecondsDiff / 1000);
3333
const minutes = Math.floor(seconds / 60);
3434
const hours = Math.floor(minutes / 60);
3535
const days = Math.floor(hours / 24);
36+
const months = Math.floor(days / 30);
3637
const years = Math.floor(days / 365);
3738

38-
if (timeDiff < 60000) return "Just now";
39+
if (millisecondsDiff < 60000) return "Just now";
3940
else if (minutes < 60)
4041
return minutes === 1 ? "1 minute ago" : `${minutes} minutes ago`;
4142
else if (hours < 24) return hours === 1 ? "1 hour ago" : `${hours} hours ago`;
42-
else if (days < 365) return days === 1 ? "1 day ago" : `${days} days ago`;
43+
else if (days < 30) return days === 1 ? "1 day ago" : `${days} days ago`;
44+
else if (months < 12)
45+
return months === 1 ? "1 month ago" : `${months} months ago`;
4346
else return years === 1 ? "1 year ago" : `${years} years ago`;
4447
};
4548

0 commit comments

Comments
 (0)