forked from Disciplr-Org/Disciplr-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelativeTime.ts
More file actions
76 lines (67 loc) · 2.35 KB
/
Copy pathrelativeTime.ts
File metadata and controls
76 lines (67 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* formatRelativeTime - Format a date as a relative time string (e.g., "2 hours ago")
*
* Uses Intl.RelativeTimeFormat for localized, human-readable time differences.
* Handles seconds, minutes, hours, days, and weeks. Supports both past and future dates.
*
* @param date - The date to format (Date object, ISO string, or timestamp)
* @param now - Optional reference time for testing (defaults to current time)
* @returns A localized relative time string (e.g., "2 hours ago", "in 3 days")
*
* @example
* formatRelativeTime(new Date(Date.now() - 3600000)) // "1 hour ago"
* formatRelativeTime(new Date(Date.now() + 86400000)) // "in 1 day"
* formatRelativeTime("2024-04-28T14:30:00Z", new Date("2024-04-28T16:30:00Z").getTime()) // "2 hours ago"
*/
export function formatRelativeTime(
date: Date | string | number,
now: number = Date.now(),
): string {
// Convert input to timestamp
const timestamp = typeof date === "number" ? date : new Date(date).getTime();
// Handle invalid dates
if (isNaN(timestamp)) {
return "Invalid date";
}
const diffInSeconds = (timestamp - now) / 1000;
const absDiff = Math.abs(diffInSeconds);
// Use Intl.RelativeTimeFormat for localized formatting
const rtf = new Intl.RelativeTimeFormat("en", { numeric: "always" });
// Less than 1 minute
if (absDiff < 60) {
if (absDiff < 10) {
return diffInSeconds > 0 ? "in a moment" : "just now";
}
return rtf.format(Math.round(diffInSeconds), "second");
}
// Less than 1 hour
if (absDiff < 3600) {
const minutes = Math.round(diffInSeconds / 60);
return rtf.format(minutes, "minute");
}
// Less than 1 day
if (absDiff < 86400) {
const hours = Math.round(diffInSeconds / 3600);
return rtf.format(hours, "hour");
}
// Less than 1 week
if (absDiff < 604800) {
const days = Math.round(diffInSeconds / 86400);
return rtf.format(days, "day");
}
// Less than 1 month (approximately 30 days)
if (absDiff < 2592000) {
const weeks = Math.round(diffInSeconds / 604800);
return rtf.format(weeks, "week");
}
// For dates beyond a month, fall back to absolute date formatting
const dateObj = new Date(timestamp);
return dateObj.toLocaleDateString("en-US", {
month: "short",
day: "numeric",
year:
dateObj.getFullYear() !== new Date(now).getFullYear()
? "numeric"
: undefined,
});
}