-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathformat_date.ts
More file actions
274 lines (247 loc) · 7.24 KB
/
format_date.ts
File metadata and controls
274 lines (247 loc) · 7.24 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import type { HassConfig } from "home-assistant-js-websocket";
import { format } from "date-fns";
import { TZDate } from "@date-fns/tz";
import type { FrontendLocaleData } from "../../data/translation";
import { DateFormat } from "../../data/translation";
import { resolveTimeZone } from "./resolve-time-zone";
// Helper to get date in target timezone
const toTimeZone = (date: Date, timeZone: string): Date => {
try {
return new TZDate(date, timeZone);
} catch {
return date;
}
};
// Helper to get format string based on date preference
const formatForDatePreference = (
template: { DMY: string; MDY: string; YMD: string },
locale: FrontendLocaleData
): string => {
if (
locale.date_format === DateFormat.language ||
locale.date_format === DateFormat.system
) {
return template.MDY; // Default to MDY for browser locale
}
const pattern =
template[locale.date_format as unknown as keyof typeof template];
return pattern || template.MDY; // Fallback to MDY if not found
};
// Tuesday, August 10
export const formatDateWeekdayDay = (
dateObj: Date,
locale: FrontendLocaleData,
config: HassConfig
) => {
const timeZone = resolveTimeZone(locale.time_zone, config.time_zone);
const zonedDate = toTimeZone(dateObj, timeZone);
const pattern = formatForDatePreference(
{ DMY: "EEEE, d MMMM", MDY: "EEEE, MMMM d", YMD: "EEEE, MMMM d" },
locale
);
return format(zonedDate, pattern);
};
// August 10, 2021
export const formatDate = (
dateObj: Date,
locale: FrontendLocaleData,
config: HassConfig
) => {
const timeZone = resolveTimeZone(locale.time_zone, config.time_zone);
const zonedDate = toTimeZone(dateObj, timeZone);
const pattern = formatForDatePreference(
{
DMY: "d MMMM, yyyy",
MDY: "MMMM d, yyyy",
YMD: "yyyy, MMMM d",
},
locale
);
return format(zonedDate, pattern);
};
// Aug 10, 2021
export const formatDateShort = (
dateObj: Date,
locale: FrontendLocaleData,
config: HassConfig
) => {
const timeZone = resolveTimeZone(locale.time_zone, config.time_zone);
const zonedDate = toTimeZone(dateObj, timeZone);
const pattern = formatForDatePreference(
{
DMY: "d MMM, yyyy",
MDY: "MMM d, yyyy",
YMD: "yyyy, MMM d",
},
locale
);
return format(zonedDate, pattern);
};
// 10/08/2021
export const formatDateNumeric = (
dateObj: Date,
locale: FrontendLocaleData,
config: HassConfig
) => {
const formatter = createDateNumericFormatter(locale, config.time_zone);
if (
locale.date_format === DateFormat.language ||
locale.date_format === DateFormat.system
) {
return formatter.format(dateObj);
}
const parts = formatter.formatToParts(dateObj);
const literal = parts.find((value) => value.type === "literal")?.value;
const day = parts.find((value) => value.type === "day")?.value;
const month = parts.find((value) => value.type === "month")?.value;
const year = parts.find((value) => value.type === "year")?.value;
const lastPart = parts[parts.length - 1];
let lastLiteral = lastPart?.type === "literal" ? lastPart?.value : "";
if (locale.language === "bg" && locale.date_format === DateFormat.YMD) {
lastLiteral = "";
}
const formats = {
[DateFormat.DMY]: `${day}${literal}${month}${literal}${year}${lastLiteral}`,
[DateFormat.MDY]: `${month}${literal}${day}${literal}${year}${lastLiteral}`,
[DateFormat.YMD]: `${year}${literal}${month}${literal}${day}${lastLiteral}`,
};
return formats[locale.date_format];
};
const createDateNumericFormatter = (
locale: FrontendLocaleData,
serverTimeZone: string
) => {
const localeString =
locale.date_format === DateFormat.system ? undefined : locale.language;
return new Intl.DateTimeFormat(localeString, {
year: "numeric",
month: "numeric",
day: "numeric",
timeZone: resolveTimeZone(locale.time_zone, serverTimeZone),
});
};
// Aug 10
export const formatDateVeryShort = (
dateObj: Date,
locale: FrontendLocaleData,
config: HassConfig
) => {
const timeZone = resolveTimeZone(locale.time_zone, config.time_zone);
const zonedDate = toTimeZone(dateObj, timeZone);
const pattern = formatForDatePreference(
{ DMY: "d MMM", MDY: "MMM d", YMD: "MMM d" },
locale
);
return format(zonedDate, pattern);
};
// August 2021
export const formatDateMonthYear = (
dateObj: Date,
locale: FrontendLocaleData,
config: HassConfig
) => {
const timeZone = resolveTimeZone(locale.time_zone, config.time_zone);
const zonedDate = toTimeZone(dateObj, timeZone);
const pattern = formatForDatePreference(
{
DMY: "MMMM yyyy",
MDY: "MMMM yyyy",
YMD: "yyyy MMMM",
},
locale
);
return format(zonedDate, pattern);
};
// August
export const formatDateMonth = (
dateObj: Date,
locale: FrontendLocaleData,
config: HassConfig
) => {
const timeZone = resolveTimeZone(locale.time_zone, config.time_zone);
const zonedDate = toTimeZone(dateObj, timeZone);
return format(zonedDate, "MMMM");
};
// Aug
export const formatDateMonthShort = (
dateObj: Date,
locale: FrontendLocaleData,
config: HassConfig
) => {
const timeZone = resolveTimeZone(locale.time_zone, config.time_zone);
const zonedDate = toTimeZone(dateObj, timeZone);
return format(zonedDate, "MMM");
};
// 2021
export const formatDateYear = (
dateObj: Date,
locale: FrontendLocaleData,
config: HassConfig
) => {
const timeZone = resolveTimeZone(locale.time_zone, config.time_zone);
const zonedDate = toTimeZone(dateObj, timeZone);
return format(zonedDate, "yyyy");
};
// Monday
export const formatDateWeekday = (
dateObj: Date,
locale: FrontendLocaleData,
config: HassConfig
) => {
const timeZone = resolveTimeZone(locale.time_zone, config.time_zone);
const zonedDate = toTimeZone(dateObj, timeZone);
return format(zonedDate, "EEEE");
};
// Mon
export const formatDateWeekdayShort = (
dateObj: Date,
locale: FrontendLocaleData,
config: HassConfig
) => {
const timeZone = resolveTimeZone(locale.time_zone, config.time_zone);
const zonedDate = toTimeZone(dateObj, timeZone);
return format(zonedDate, "EEE");
};
// Mon, Aug 10
export const formatDateWeekdayVeryShortDate = (
dateObj: Date,
locale: FrontendLocaleData,
config: HassConfig
) => {
const timeZone = resolveTimeZone(locale.time_zone, config.time_zone);
const zonedDate = toTimeZone(dateObj, timeZone);
return format(zonedDate, "EEE, MMM d");
};
// Mon, Aug 10, 2021
export const formatDateWeekdayShortDate = (
dateObj: Date,
locale: FrontendLocaleData,
config: HassConfig
) => {
const timeZone = resolveTimeZone(locale.time_zone, config.time_zone);
const zonedDate = toTimeZone(dateObj, timeZone);
return format(zonedDate, "EEE, MMM d, yyyy");
};
/**
* Format a date as YYYY-MM-DD
*/
export const formatISODateOnly = (
dateObj: Date,
locale: FrontendLocaleData,
config: HassConfig
) => {
const timeZone = resolveTimeZone(locale.time_zone, config.time_zone);
const zonedDate = toTimeZone(dateObj, timeZone);
return format(zonedDate, "yyyy-MM-dd");
};
// 2026-08-10/2026-08-15
export const formatCallyDateRange = (
start: Date,
end: Date,
locale: FrontendLocaleData,
config: HassConfig
) => {
const startDate = formatISODateOnly(start, locale, config);
const endDate = formatISODateOnly(end, locale, config);
return `${startDate}/${endDate}`;
};