-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathformat-time.ts
More file actions
164 lines (141 loc) · 4.3 KB
/
format-time.ts
File metadata and controls
164 lines (141 loc) · 4.3 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import {
format,
isSameDay,
isSameMinute,
isSameYear,
startOfDay,
} from "date-fns";
import dayjs, { type Dayjs } from "dayjs";
import duration from "dayjs/plugin/duration";
import utc from "dayjs/plugin/utc";
import { formatDateRange } from "little-date";
dayjs.extend(utc);
dayjs.extend(duration);
const shortenAmPm = (text: string): string => {
const shortened = (text || "").replace(/ AM/g, "am").replace(/ PM/g, "pm");
const withoutDoubleZero = shortened.includes("m")
? shortened.replace(/:00/g, "")
: shortened;
return withoutDoubleZero;
};
const removeLeadingZero = (text: string): string => text.replace(/^0/, "");
const formatTime = (date: Date, locale?: string): string => {
return removeLeadingZero(
shortenAmPm(
date.toLocaleTimeString(locale, {
hour: "2-digit",
minute: "2-digit",
}) || "",
),
);
};
const createFormatTime =
(locale?: string) =>
(date: Date): string =>
formatTime(date, locale);
export const formatDate = (
date: Date,
{
today = new Date(),
showToday = true,
forceIncludeTime = false,
}: {
showToday?: boolean;
forceIncludeTime?: boolean;
today?: Date;
} = {},
): string => {
const thisYear = isSameYear(date, today);
const thisDay = isSameDay(date, today);
const formatTimeWithLocale = createFormatTime("en-US");
const timeSuffix =
!isSameMinute(startOfDay(date), date) || forceIncludeTime
? `, ${formatTimeWithLocale(date)}`
: "";
const yearSuffix = thisYear ? "" : `, ${format(date, "yyyy")}`;
// If it's today and we have time, just show the time
if (thisDay && timeSuffix) {
if (showToday) {
return `Today, ${formatTimeWithLocale(date)}`;
}
return formatTimeWithLocale(date);
}
// Standard date format
return `${format(date, "LLL d")}${timeSuffix}${yearSuffix}`;
};
export const formatNullableDateRange = (
startDate: Dayjs | null | undefined,
endDate: Dayjs | null | undefined,
): string => {
let startEnd = "";
if (startDate && endDate) {
startEnd = formatDateRange(startDate.toDate(), endDate.toDate(), {
includeTime: true,
separator: "→",
});
if (startEnd.includes("'")) {
startEnd = formatDateRange(startDate.toDate(), endDate.toDate(), {
includeTime: false,
separator: "→",
});
}
} else if (startDate) {
startEnd = `${formatDate(startDate.toDate())} → ?`;
} else {
startEnd = "Not available";
}
if (startDate) {
const thisDay = isSameDay(startDate.toDate(), new Date());
if (thisDay) {
startEnd = `Today, ${startEnd}`;
}
}
return startEnd;
};
/**
* Formats a dayjs date in UTC timezone.
* This is needed because formatDate() uses toLocaleTimeString() which always
* formats in local timezone. This function formats directly in UTC using dayjs.
*
* Only shows the date if UTC falls on a different calendar day than local
* (e.g., "Today, 8pm PST Feb 6, 4am UTC"). If same day, just shows time
* (e.g., "Today, 10am PST 6pm UTC").
*/
export const formatDateAsUTC = (date: Dayjs): string => {
const utcDate = date.utc();
const localDate = date;
// Format time: "4pm" or "4:30pm"
const timeStr =
utcDate.minute() === 0 ? utcDate.format("ha") : utcDate.format("h:mma");
// Only show date if UTC and local fall on different calendar days
const sameCalendarDay =
utcDate.date() === localDate.date() &&
utcDate.month() === localDate.month() &&
utcDate.year() === localDate.year();
if (sameCalendarDay) {
return `${timeStr} UTC`;
}
return `${utcDate.format("MMM D")}, ${timeStr} UTC`;
};
/**
* Formats a duration in milliseconds to a compact string like "1d2h30m".
*/
export function formatDuration(ms: number) {
const d = dayjs.duration(ms);
const years = Math.floor(d.asYears());
const weeks = Math.floor(d.asWeeks()) % 52;
const days = d.days();
const hours = d.hours();
const minutes = d.minutes();
const seconds = d.seconds();
const milliseconds = d.milliseconds();
let result = "";
if (years > 0) result += `${years}y`;
if (weeks > 0) result += `${weeks}w`;
if (days > 0) result += `${days}d`;
if (hours > 0) result += `${hours}h`;
if (minutes > 0) result += `${minutes}m`;
if (seconds > 0) result += `${seconds}s`;
if (milliseconds > 0) result += `${milliseconds}ms`;
return result || "0ms";
}