@dialiq/calendar-component 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +223 -0
- package/dist/Calendar.d.ts +6 -0
- package/dist/Calendar.d.ts.map +1 -0
- package/dist/CreateBookingModal.d.ts +14 -0
- package/dist/CreateBookingModal.d.ts.map +1 -0
- package/dist/api.d.ts +6 -0
- package/dist/api.d.ts.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.esm.js +3674 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/index.js +3680 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +34 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +45 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,3680 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var React = require('react');
|
|
4
|
+
|
|
5
|
+
/******************************************************************************
|
|
6
|
+
Copyright (c) Microsoft Corporation.
|
|
7
|
+
|
|
8
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
9
|
+
purpose with or without fee is hereby granted.
|
|
10
|
+
|
|
11
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
12
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
13
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
14
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
15
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
16
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
17
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
18
|
+
***************************************************************************** */
|
|
19
|
+
/* global Reflect, Promise, SuppressedError, Symbol, Iterator */
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
23
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
24
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
25
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
26
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
27
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
28
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
33
|
+
var e = new Error(message);
|
|
34
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @name toDate
|
|
39
|
+
* @category Common Helpers
|
|
40
|
+
* @summary Convert the given argument to an instance of Date.
|
|
41
|
+
*
|
|
42
|
+
* @description
|
|
43
|
+
* Convert the given argument to an instance of Date.
|
|
44
|
+
*
|
|
45
|
+
* If the argument is an instance of Date, the function returns its clone.
|
|
46
|
+
*
|
|
47
|
+
* If the argument is a number, it is treated as a timestamp.
|
|
48
|
+
*
|
|
49
|
+
* If the argument is none of the above, the function returns Invalid Date.
|
|
50
|
+
*
|
|
51
|
+
* **Note**: *all* Date arguments passed to any *date-fns* function is processed by `toDate`.
|
|
52
|
+
*
|
|
53
|
+
* @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
|
|
54
|
+
*
|
|
55
|
+
* @param argument - The value to convert
|
|
56
|
+
*
|
|
57
|
+
* @returns The parsed date in the local time zone
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* // Clone the date:
|
|
61
|
+
* const result = toDate(new Date(2014, 1, 11, 11, 30, 30))
|
|
62
|
+
* //=> Tue Feb 11 2014 11:30:30
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* // Convert the timestamp to date:
|
|
66
|
+
* const result = toDate(1392098430000)
|
|
67
|
+
* //=> Tue Feb 11 2014 11:30:30
|
|
68
|
+
*/
|
|
69
|
+
function toDate(argument) {
|
|
70
|
+
const argStr = Object.prototype.toString.call(argument);
|
|
71
|
+
|
|
72
|
+
// Clone the date
|
|
73
|
+
if (
|
|
74
|
+
argument instanceof Date ||
|
|
75
|
+
(typeof argument === "object" && argStr === "[object Date]")
|
|
76
|
+
) {
|
|
77
|
+
// Prevent the date to lose the milliseconds when passed to new Date() in IE10
|
|
78
|
+
return new argument.constructor(+argument);
|
|
79
|
+
} else if (
|
|
80
|
+
typeof argument === "number" ||
|
|
81
|
+
argStr === "[object Number]" ||
|
|
82
|
+
typeof argument === "string" ||
|
|
83
|
+
argStr === "[object String]"
|
|
84
|
+
) {
|
|
85
|
+
// TODO: Can we get rid of as?
|
|
86
|
+
return new Date(argument);
|
|
87
|
+
} else {
|
|
88
|
+
// TODO: Can we get rid of as?
|
|
89
|
+
return new Date(NaN);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* @name constructFrom
|
|
95
|
+
* @category Generic Helpers
|
|
96
|
+
* @summary Constructs a date using the reference date and the value
|
|
97
|
+
*
|
|
98
|
+
* @description
|
|
99
|
+
* The function constructs a new date using the constructor from the reference
|
|
100
|
+
* date and the given value. It helps to build generic functions that accept
|
|
101
|
+
* date extensions.
|
|
102
|
+
*
|
|
103
|
+
* It defaults to `Date` if the passed reference date is a number or a string.
|
|
104
|
+
*
|
|
105
|
+
* @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
|
|
106
|
+
*
|
|
107
|
+
* @param date - The reference date to take constructor from
|
|
108
|
+
* @param value - The value to create the date
|
|
109
|
+
*
|
|
110
|
+
* @returns Date initialized using the given date and value
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* import { constructFrom } from 'date-fns'
|
|
114
|
+
*
|
|
115
|
+
* // A function that clones a date preserving the original type
|
|
116
|
+
* function cloneDate<DateType extends Date(date: DateType): DateType {
|
|
117
|
+
* return constructFrom(
|
|
118
|
+
* date, // Use contrustor from the given date
|
|
119
|
+
* date.getTime() // Use the date value to create a new date
|
|
120
|
+
* )
|
|
121
|
+
* }
|
|
122
|
+
*/
|
|
123
|
+
function constructFrom(date, value) {
|
|
124
|
+
if (date instanceof Date) {
|
|
125
|
+
return new date.constructor(value);
|
|
126
|
+
} else {
|
|
127
|
+
return new Date(value);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* @name addDays
|
|
133
|
+
* @category Day Helpers
|
|
134
|
+
* @summary Add the specified number of days to the given date.
|
|
135
|
+
*
|
|
136
|
+
* @description
|
|
137
|
+
* Add the specified number of days to the given date.
|
|
138
|
+
*
|
|
139
|
+
* @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
|
|
140
|
+
*
|
|
141
|
+
* @param date - The date to be changed
|
|
142
|
+
* @param amount - The amount of days to be added.
|
|
143
|
+
*
|
|
144
|
+
* @returns The new date with the days added
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* // Add 10 days to 1 September 2014:
|
|
148
|
+
* const result = addDays(new Date(2014, 8, 1), 10)
|
|
149
|
+
* //=> Thu Sep 11 2014 00:00:00
|
|
150
|
+
*/
|
|
151
|
+
function addDays(date, amount) {
|
|
152
|
+
const _date = toDate(date);
|
|
153
|
+
if (isNaN(amount)) return constructFrom(date, NaN);
|
|
154
|
+
if (!amount) {
|
|
155
|
+
// If 0 days, no-op to avoid changing times in the hour before end of DST
|
|
156
|
+
return _date;
|
|
157
|
+
}
|
|
158
|
+
_date.setDate(_date.getDate() + amount);
|
|
159
|
+
return _date;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* @name addMonths
|
|
164
|
+
* @category Month Helpers
|
|
165
|
+
* @summary Add the specified number of months to the given date.
|
|
166
|
+
*
|
|
167
|
+
* @description
|
|
168
|
+
* Add the specified number of months to the given date.
|
|
169
|
+
*
|
|
170
|
+
* @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
|
|
171
|
+
*
|
|
172
|
+
* @param date - The date to be changed
|
|
173
|
+
* @param amount - The amount of months to be added.
|
|
174
|
+
*
|
|
175
|
+
* @returns The new date with the months added
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* // Add 5 months to 1 September 2014:
|
|
179
|
+
* const result = addMonths(new Date(2014, 8, 1), 5)
|
|
180
|
+
* //=> Sun Feb 01 2015 00:00:00
|
|
181
|
+
*
|
|
182
|
+
* // Add one month to 30 January 2023:
|
|
183
|
+
* const result = addMonths(new Date(2023, 0, 30), 1)
|
|
184
|
+
* //=> Tue Feb 28 2023 00:00:00
|
|
185
|
+
*/
|
|
186
|
+
function addMonths(date, amount) {
|
|
187
|
+
const _date = toDate(date);
|
|
188
|
+
if (isNaN(amount)) return constructFrom(date, NaN);
|
|
189
|
+
if (!amount) {
|
|
190
|
+
// If 0 months, no-op to avoid changing times in the hour before end of DST
|
|
191
|
+
return _date;
|
|
192
|
+
}
|
|
193
|
+
const dayOfMonth = _date.getDate();
|
|
194
|
+
|
|
195
|
+
// The JS Date object supports date math by accepting out-of-bounds values for
|
|
196
|
+
// month, day, etc. For example, new Date(2020, 0, 0) returns 31 Dec 2019 and
|
|
197
|
+
// new Date(2020, 13, 1) returns 1 Feb 2021. This is *almost* the behavior we
|
|
198
|
+
// want except that dates will wrap around the end of a month, meaning that
|
|
199
|
+
// new Date(2020, 13, 31) will return 3 Mar 2021 not 28 Feb 2021 as desired. So
|
|
200
|
+
// we'll default to the end of the desired month by adding 1 to the desired
|
|
201
|
+
// month and using a date of 0 to back up one day to the end of the desired
|
|
202
|
+
// month.
|
|
203
|
+
const endOfDesiredMonth = constructFrom(date, _date.getTime());
|
|
204
|
+
endOfDesiredMonth.setMonth(_date.getMonth() + amount + 1, 0);
|
|
205
|
+
const daysInMonth = endOfDesiredMonth.getDate();
|
|
206
|
+
if (dayOfMonth >= daysInMonth) {
|
|
207
|
+
// If we're already at the end of the month, then this is the correct date
|
|
208
|
+
// and we're done.
|
|
209
|
+
return endOfDesiredMonth;
|
|
210
|
+
} else {
|
|
211
|
+
// Otherwise, we now know that setting the original day-of-month value won't
|
|
212
|
+
// cause an overflow, so set the desired day-of-month. Note that we can't
|
|
213
|
+
// just set the date of `endOfDesiredMonth` because that object may have had
|
|
214
|
+
// its time changed in the unusual case where where a DST transition was on
|
|
215
|
+
// the last day of the month and its local time was in the hour skipped or
|
|
216
|
+
// repeated next to a DST transition. So we use `date` instead which is
|
|
217
|
+
// guaranteed to still have the original time.
|
|
218
|
+
_date.setFullYear(
|
|
219
|
+
endOfDesiredMonth.getFullYear(),
|
|
220
|
+
endOfDesiredMonth.getMonth(),
|
|
221
|
+
dayOfMonth,
|
|
222
|
+
);
|
|
223
|
+
return _date;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* @module constants
|
|
229
|
+
* @summary Useful constants
|
|
230
|
+
* @description
|
|
231
|
+
* Collection of useful date constants.
|
|
232
|
+
*
|
|
233
|
+
* The constants could be imported from `date-fns/constants`:
|
|
234
|
+
*
|
|
235
|
+
* ```ts
|
|
236
|
+
* import { maxTime, minTime } from "./constants/date-fns/constants";
|
|
237
|
+
*
|
|
238
|
+
* function isAllowedTime(time) {
|
|
239
|
+
* return time <= maxTime && time >= minTime;
|
|
240
|
+
* }
|
|
241
|
+
* ```
|
|
242
|
+
*/
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* @constant
|
|
247
|
+
* @name millisecondsInWeek
|
|
248
|
+
* @summary Milliseconds in 1 week.
|
|
249
|
+
*/
|
|
250
|
+
const millisecondsInWeek = 604800000;
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* @constant
|
|
254
|
+
* @name millisecondsInDay
|
|
255
|
+
* @summary Milliseconds in 1 day.
|
|
256
|
+
*/
|
|
257
|
+
const millisecondsInDay = 86400000;
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* @constant
|
|
261
|
+
* @name millisecondsInMinute
|
|
262
|
+
* @summary Milliseconds in 1 minute
|
|
263
|
+
*/
|
|
264
|
+
const millisecondsInMinute = 60000;
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* @constant
|
|
268
|
+
* @name millisecondsInHour
|
|
269
|
+
* @summary Milliseconds in 1 hour
|
|
270
|
+
*/
|
|
271
|
+
const millisecondsInHour = 3600000;
|
|
272
|
+
|
|
273
|
+
let defaultOptions = {};
|
|
274
|
+
|
|
275
|
+
function getDefaultOptions() {
|
|
276
|
+
return defaultOptions;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* The {@link startOfWeek} function options.
|
|
281
|
+
*/
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* @name startOfWeek
|
|
285
|
+
* @category Week Helpers
|
|
286
|
+
* @summary Return the start of a week for the given date.
|
|
287
|
+
*
|
|
288
|
+
* @description
|
|
289
|
+
* Return the start of a week for the given date.
|
|
290
|
+
* The result will be in the local timezone.
|
|
291
|
+
*
|
|
292
|
+
* @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
|
|
293
|
+
*
|
|
294
|
+
* @param date - The original date
|
|
295
|
+
* @param options - An object with options
|
|
296
|
+
*
|
|
297
|
+
* @returns The start of a week
|
|
298
|
+
*
|
|
299
|
+
* @example
|
|
300
|
+
* // The start of a week for 2 September 2014 11:55:00:
|
|
301
|
+
* const result = startOfWeek(new Date(2014, 8, 2, 11, 55, 0))
|
|
302
|
+
* //=> Sun Aug 31 2014 00:00:00
|
|
303
|
+
*
|
|
304
|
+
* @example
|
|
305
|
+
* // If the week starts on Monday, the start of the week for 2 September 2014 11:55:00:
|
|
306
|
+
* const result = startOfWeek(new Date(2014, 8, 2, 11, 55, 0), { weekStartsOn: 1 })
|
|
307
|
+
* //=> Mon Sep 01 2014 00:00:00
|
|
308
|
+
*/
|
|
309
|
+
function startOfWeek(date, options) {
|
|
310
|
+
const defaultOptions = getDefaultOptions();
|
|
311
|
+
const weekStartsOn =
|
|
312
|
+
options?.weekStartsOn ??
|
|
313
|
+
options?.locale?.options?.weekStartsOn ??
|
|
314
|
+
defaultOptions.weekStartsOn ??
|
|
315
|
+
defaultOptions.locale?.options?.weekStartsOn ??
|
|
316
|
+
0;
|
|
317
|
+
|
|
318
|
+
const _date = toDate(date);
|
|
319
|
+
const day = _date.getDay();
|
|
320
|
+
const diff = (day < weekStartsOn ? 7 : 0) + day - weekStartsOn;
|
|
321
|
+
|
|
322
|
+
_date.setDate(_date.getDate() - diff);
|
|
323
|
+
_date.setHours(0, 0, 0, 0);
|
|
324
|
+
return _date;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* @name startOfISOWeek
|
|
329
|
+
* @category ISO Week Helpers
|
|
330
|
+
* @summary Return the start of an ISO week for the given date.
|
|
331
|
+
*
|
|
332
|
+
* @description
|
|
333
|
+
* Return the start of an ISO week for the given date.
|
|
334
|
+
* The result will be in the local timezone.
|
|
335
|
+
*
|
|
336
|
+
* ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
|
|
337
|
+
*
|
|
338
|
+
* @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
|
|
339
|
+
*
|
|
340
|
+
* @param date - The original date
|
|
341
|
+
*
|
|
342
|
+
* @returns The start of an ISO week
|
|
343
|
+
*
|
|
344
|
+
* @example
|
|
345
|
+
* // The start of an ISO week for 2 September 2014 11:55:00:
|
|
346
|
+
* const result = startOfISOWeek(new Date(2014, 8, 2, 11, 55, 0))
|
|
347
|
+
* //=> Mon Sep 01 2014 00:00:00
|
|
348
|
+
*/
|
|
349
|
+
function startOfISOWeek(date) {
|
|
350
|
+
return startOfWeek(date, { weekStartsOn: 1 });
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* @name getISOWeekYear
|
|
355
|
+
* @category ISO Week-Numbering Year Helpers
|
|
356
|
+
* @summary Get the ISO week-numbering year of the given date.
|
|
357
|
+
*
|
|
358
|
+
* @description
|
|
359
|
+
* Get the ISO week-numbering year of the given date,
|
|
360
|
+
* which always starts 3 days before the year's first Thursday.
|
|
361
|
+
*
|
|
362
|
+
* ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
|
|
363
|
+
*
|
|
364
|
+
* @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
|
|
365
|
+
*
|
|
366
|
+
* @param date - The given date
|
|
367
|
+
*
|
|
368
|
+
* @returns The ISO week-numbering year
|
|
369
|
+
*
|
|
370
|
+
* @example
|
|
371
|
+
* // Which ISO-week numbering year is 2 January 2005?
|
|
372
|
+
* const result = getISOWeekYear(new Date(2005, 0, 2))
|
|
373
|
+
* //=> 2004
|
|
374
|
+
*/
|
|
375
|
+
function getISOWeekYear(date) {
|
|
376
|
+
const _date = toDate(date);
|
|
377
|
+
const year = _date.getFullYear();
|
|
378
|
+
|
|
379
|
+
const fourthOfJanuaryOfNextYear = constructFrom(date, 0);
|
|
380
|
+
fourthOfJanuaryOfNextYear.setFullYear(year + 1, 0, 4);
|
|
381
|
+
fourthOfJanuaryOfNextYear.setHours(0, 0, 0, 0);
|
|
382
|
+
const startOfNextYear = startOfISOWeek(fourthOfJanuaryOfNextYear);
|
|
383
|
+
|
|
384
|
+
const fourthOfJanuaryOfThisYear = constructFrom(date, 0);
|
|
385
|
+
fourthOfJanuaryOfThisYear.setFullYear(year, 0, 4);
|
|
386
|
+
fourthOfJanuaryOfThisYear.setHours(0, 0, 0, 0);
|
|
387
|
+
const startOfThisYear = startOfISOWeek(fourthOfJanuaryOfThisYear);
|
|
388
|
+
|
|
389
|
+
if (_date.getTime() >= startOfNextYear.getTime()) {
|
|
390
|
+
return year + 1;
|
|
391
|
+
} else if (_date.getTime() >= startOfThisYear.getTime()) {
|
|
392
|
+
return year;
|
|
393
|
+
} else {
|
|
394
|
+
return year - 1;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* @name startOfDay
|
|
400
|
+
* @category Day Helpers
|
|
401
|
+
* @summary Return the start of a day for the given date.
|
|
402
|
+
*
|
|
403
|
+
* @description
|
|
404
|
+
* Return the start of a day for the given date.
|
|
405
|
+
* The result will be in the local timezone.
|
|
406
|
+
*
|
|
407
|
+
* @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
|
|
408
|
+
*
|
|
409
|
+
* @param date - The original date
|
|
410
|
+
*
|
|
411
|
+
* @returns The start of a day
|
|
412
|
+
*
|
|
413
|
+
* @example
|
|
414
|
+
* // The start of a day for 2 September 2014 11:55:00:
|
|
415
|
+
* const result = startOfDay(new Date(2014, 8, 2, 11, 55, 0))
|
|
416
|
+
* //=> Tue Sep 02 2014 00:00:00
|
|
417
|
+
*/
|
|
418
|
+
function startOfDay(date) {
|
|
419
|
+
const _date = toDate(date);
|
|
420
|
+
_date.setHours(0, 0, 0, 0);
|
|
421
|
+
return _date;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* Google Chrome as of 67.0.3396.87 introduced timezones with offset that includes seconds.
|
|
426
|
+
* They usually appear for dates that denote time before the timezones were introduced
|
|
427
|
+
* (e.g. for 'Europe/Prague' timezone the offset is GMT+00:57:44 before 1 October 1891
|
|
428
|
+
* and GMT+01:00:00 after that date)
|
|
429
|
+
*
|
|
430
|
+
* Date#getTimezoneOffset returns the offset in minutes and would return 57 for the example above,
|
|
431
|
+
* which would lead to incorrect calculations.
|
|
432
|
+
*
|
|
433
|
+
* This function returns the timezone offset in milliseconds that takes seconds in account.
|
|
434
|
+
*/
|
|
435
|
+
function getTimezoneOffsetInMilliseconds(date) {
|
|
436
|
+
const _date = toDate(date);
|
|
437
|
+
const utcDate = new Date(
|
|
438
|
+
Date.UTC(
|
|
439
|
+
_date.getFullYear(),
|
|
440
|
+
_date.getMonth(),
|
|
441
|
+
_date.getDate(),
|
|
442
|
+
_date.getHours(),
|
|
443
|
+
_date.getMinutes(),
|
|
444
|
+
_date.getSeconds(),
|
|
445
|
+
_date.getMilliseconds(),
|
|
446
|
+
),
|
|
447
|
+
);
|
|
448
|
+
utcDate.setUTCFullYear(_date.getFullYear());
|
|
449
|
+
return +date - +utcDate;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* @name differenceInCalendarDays
|
|
454
|
+
* @category Day Helpers
|
|
455
|
+
* @summary Get the number of calendar days between the given dates.
|
|
456
|
+
*
|
|
457
|
+
* @description
|
|
458
|
+
* Get the number of calendar days between the given dates. This means that the times are removed
|
|
459
|
+
* from the dates and then the difference in days is calculated.
|
|
460
|
+
*
|
|
461
|
+
* @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
|
|
462
|
+
*
|
|
463
|
+
* @param dateLeft - The later date
|
|
464
|
+
* @param dateRight - The earlier date
|
|
465
|
+
*
|
|
466
|
+
* @returns The number of calendar days
|
|
467
|
+
*
|
|
468
|
+
* @example
|
|
469
|
+
* // How many calendar days are between
|
|
470
|
+
* // 2 July 2011 23:00:00 and 2 July 2012 00:00:00?
|
|
471
|
+
* const result = differenceInCalendarDays(
|
|
472
|
+
* new Date(2012, 6, 2, 0, 0),
|
|
473
|
+
* new Date(2011, 6, 2, 23, 0)
|
|
474
|
+
* )
|
|
475
|
+
* //=> 366
|
|
476
|
+
* // How many calendar days are between
|
|
477
|
+
* // 2 July 2011 23:59:00 and 3 July 2011 00:01:00?
|
|
478
|
+
* const result = differenceInCalendarDays(
|
|
479
|
+
* new Date(2011, 6, 3, 0, 1),
|
|
480
|
+
* new Date(2011, 6, 2, 23, 59)
|
|
481
|
+
* )
|
|
482
|
+
* //=> 1
|
|
483
|
+
*/
|
|
484
|
+
function differenceInCalendarDays(dateLeft, dateRight) {
|
|
485
|
+
const startOfDayLeft = startOfDay(dateLeft);
|
|
486
|
+
const startOfDayRight = startOfDay(dateRight);
|
|
487
|
+
|
|
488
|
+
const timestampLeft =
|
|
489
|
+
+startOfDayLeft - getTimezoneOffsetInMilliseconds(startOfDayLeft);
|
|
490
|
+
const timestampRight =
|
|
491
|
+
+startOfDayRight - getTimezoneOffsetInMilliseconds(startOfDayRight);
|
|
492
|
+
|
|
493
|
+
// Round the number of days to the nearest integer because the number of
|
|
494
|
+
// milliseconds in a day is not constant (e.g. it's different in the week of
|
|
495
|
+
// the daylight saving time clock shift).
|
|
496
|
+
return Math.round((timestampLeft - timestampRight) / millisecondsInDay);
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* @name startOfISOWeekYear
|
|
501
|
+
* @category ISO Week-Numbering Year Helpers
|
|
502
|
+
* @summary Return the start of an ISO week-numbering year for the given date.
|
|
503
|
+
*
|
|
504
|
+
* @description
|
|
505
|
+
* Return the start of an ISO week-numbering year,
|
|
506
|
+
* which always starts 3 days before the year's first Thursday.
|
|
507
|
+
* The result will be in the local timezone.
|
|
508
|
+
*
|
|
509
|
+
* ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
|
|
510
|
+
*
|
|
511
|
+
* @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
|
|
512
|
+
*
|
|
513
|
+
* @param date - The original date
|
|
514
|
+
*
|
|
515
|
+
* @returns The start of an ISO week-numbering year
|
|
516
|
+
*
|
|
517
|
+
* @example
|
|
518
|
+
* // The start of an ISO week-numbering year for 2 July 2005:
|
|
519
|
+
* const result = startOfISOWeekYear(new Date(2005, 6, 2))
|
|
520
|
+
* //=> Mon Jan 03 2005 00:00:00
|
|
521
|
+
*/
|
|
522
|
+
function startOfISOWeekYear(date) {
|
|
523
|
+
const year = getISOWeekYear(date);
|
|
524
|
+
const fourthOfJanuary = constructFrom(date, 0);
|
|
525
|
+
fourthOfJanuary.setFullYear(year, 0, 4);
|
|
526
|
+
fourthOfJanuary.setHours(0, 0, 0, 0);
|
|
527
|
+
return startOfISOWeek(fourthOfJanuary);
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
/**
|
|
531
|
+
* @name isSameDay
|
|
532
|
+
* @category Day Helpers
|
|
533
|
+
* @summary Are the given dates in the same day (and year and month)?
|
|
534
|
+
*
|
|
535
|
+
* @description
|
|
536
|
+
* Are the given dates in the same day (and year and month)?
|
|
537
|
+
*
|
|
538
|
+
* @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
|
|
539
|
+
*
|
|
540
|
+
* @param dateLeft - The first date to check
|
|
541
|
+
* @param dateRight - The second date to check
|
|
542
|
+
|
|
543
|
+
* @returns The dates are in the same day (and year and month)
|
|
544
|
+
*
|
|
545
|
+
* @example
|
|
546
|
+
* // Are 4 September 06:00:00 and 4 September 18:00:00 in the same day?
|
|
547
|
+
* const result = isSameDay(new Date(2014, 8, 4, 6, 0), new Date(2014, 8, 4, 18, 0))
|
|
548
|
+
* //=> true
|
|
549
|
+
*
|
|
550
|
+
* @example
|
|
551
|
+
* // Are 4 September and 4 October in the same day?
|
|
552
|
+
* const result = isSameDay(new Date(2014, 8, 4), new Date(2014, 9, 4))
|
|
553
|
+
* //=> false
|
|
554
|
+
*
|
|
555
|
+
* @example
|
|
556
|
+
* // Are 4 September, 2014 and 4 September, 2015 in the same day?
|
|
557
|
+
* const result = isSameDay(new Date(2014, 8, 4), new Date(2015, 8, 4))
|
|
558
|
+
* //=> false
|
|
559
|
+
*/
|
|
560
|
+
function isSameDay(dateLeft, dateRight) {
|
|
561
|
+
const dateLeftStartOfDay = startOfDay(dateLeft);
|
|
562
|
+
const dateRightStartOfDay = startOfDay(dateRight);
|
|
563
|
+
|
|
564
|
+
return +dateLeftStartOfDay === +dateRightStartOfDay;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
/**
|
|
568
|
+
* @name isDate
|
|
569
|
+
* @category Common Helpers
|
|
570
|
+
* @summary Is the given value a date?
|
|
571
|
+
*
|
|
572
|
+
* @description
|
|
573
|
+
* Returns true if the given value is an instance of Date. The function works for dates transferred across iframes.
|
|
574
|
+
*
|
|
575
|
+
* @param value - The value to check
|
|
576
|
+
*
|
|
577
|
+
* @returns True if the given value is a date
|
|
578
|
+
*
|
|
579
|
+
* @example
|
|
580
|
+
* // For a valid date:
|
|
581
|
+
* const result = isDate(new Date())
|
|
582
|
+
* //=> true
|
|
583
|
+
*
|
|
584
|
+
* @example
|
|
585
|
+
* // For an invalid date:
|
|
586
|
+
* const result = isDate(new Date(NaN))
|
|
587
|
+
* //=> true
|
|
588
|
+
*
|
|
589
|
+
* @example
|
|
590
|
+
* // For some value:
|
|
591
|
+
* const result = isDate('2014-02-31')
|
|
592
|
+
* //=> false
|
|
593
|
+
*
|
|
594
|
+
* @example
|
|
595
|
+
* // For an object:
|
|
596
|
+
* const result = isDate({})
|
|
597
|
+
* //=> false
|
|
598
|
+
*/
|
|
599
|
+
function isDate(value) {
|
|
600
|
+
return (
|
|
601
|
+
value instanceof Date ||
|
|
602
|
+
(typeof value === "object" &&
|
|
603
|
+
Object.prototype.toString.call(value) === "[object Date]")
|
|
604
|
+
);
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
/**
|
|
608
|
+
* @name isValid
|
|
609
|
+
* @category Common Helpers
|
|
610
|
+
* @summary Is the given date valid?
|
|
611
|
+
*
|
|
612
|
+
* @description
|
|
613
|
+
* Returns false if argument is Invalid Date and true otherwise.
|
|
614
|
+
* Argument is converted to Date using `toDate`. See [toDate](https://date-fns.org/docs/toDate)
|
|
615
|
+
* Invalid Date is a Date, whose time value is NaN.
|
|
616
|
+
*
|
|
617
|
+
* Time value of Date: http://es5.github.io/#x15.9.1.1
|
|
618
|
+
*
|
|
619
|
+
* @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
|
|
620
|
+
*
|
|
621
|
+
* @param date - The date to check
|
|
622
|
+
*
|
|
623
|
+
* @returns The date is valid
|
|
624
|
+
*
|
|
625
|
+
* @example
|
|
626
|
+
* // For the valid date:
|
|
627
|
+
* const result = isValid(new Date(2014, 1, 31))
|
|
628
|
+
* //=> true
|
|
629
|
+
*
|
|
630
|
+
* @example
|
|
631
|
+
* // For the value, convertable into a date:
|
|
632
|
+
* const result = isValid(1393804800000)
|
|
633
|
+
* //=> true
|
|
634
|
+
*
|
|
635
|
+
* @example
|
|
636
|
+
* // For the invalid date:
|
|
637
|
+
* const result = isValid(new Date(''))
|
|
638
|
+
* //=> false
|
|
639
|
+
*/
|
|
640
|
+
function isValid(date) {
|
|
641
|
+
if (!isDate(date) && typeof date !== "number") {
|
|
642
|
+
return false;
|
|
643
|
+
}
|
|
644
|
+
const _date = toDate(date);
|
|
645
|
+
return !isNaN(Number(_date));
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
/**
|
|
649
|
+
* @name endOfMonth
|
|
650
|
+
* @category Month Helpers
|
|
651
|
+
* @summary Return the end of a month for the given date.
|
|
652
|
+
*
|
|
653
|
+
* @description
|
|
654
|
+
* Return the end of a month for the given date.
|
|
655
|
+
* The result will be in the local timezone.
|
|
656
|
+
*
|
|
657
|
+
* @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
|
|
658
|
+
*
|
|
659
|
+
* @param date - The original date
|
|
660
|
+
*
|
|
661
|
+
* @returns The end of a month
|
|
662
|
+
*
|
|
663
|
+
* @example
|
|
664
|
+
* // The end of a month for 2 September 2014 11:55:00:
|
|
665
|
+
* const result = endOfMonth(new Date(2014, 8, 2, 11, 55, 0))
|
|
666
|
+
* //=> Tue Sep 30 2014 23:59:59.999
|
|
667
|
+
*/
|
|
668
|
+
function endOfMonth(date) {
|
|
669
|
+
const _date = toDate(date);
|
|
670
|
+
const month = _date.getMonth();
|
|
671
|
+
_date.setFullYear(_date.getFullYear(), month + 1, 0);
|
|
672
|
+
_date.setHours(23, 59, 59, 999);
|
|
673
|
+
return _date;
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
/**
|
|
677
|
+
* @name startOfMonth
|
|
678
|
+
* @category Month Helpers
|
|
679
|
+
* @summary Return the start of a month for the given date.
|
|
680
|
+
*
|
|
681
|
+
* @description
|
|
682
|
+
* Return the start of a month for the given date.
|
|
683
|
+
* The result will be in the local timezone.
|
|
684
|
+
*
|
|
685
|
+
* @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
|
|
686
|
+
*
|
|
687
|
+
* @param date - The original date
|
|
688
|
+
*
|
|
689
|
+
* @returns The start of a month
|
|
690
|
+
*
|
|
691
|
+
* @example
|
|
692
|
+
* // The start of a month for 2 September 2014 11:55:00:
|
|
693
|
+
* const result = startOfMonth(new Date(2014, 8, 2, 11, 55, 0))
|
|
694
|
+
* //=> Mon Sep 01 2014 00:00:00
|
|
695
|
+
*/
|
|
696
|
+
function startOfMonth(date) {
|
|
697
|
+
const _date = toDate(date);
|
|
698
|
+
_date.setDate(1);
|
|
699
|
+
_date.setHours(0, 0, 0, 0);
|
|
700
|
+
return _date;
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
/**
|
|
704
|
+
* @name startOfYear
|
|
705
|
+
* @category Year Helpers
|
|
706
|
+
* @summary Return the start of a year for the given date.
|
|
707
|
+
*
|
|
708
|
+
* @description
|
|
709
|
+
* Return the start of a year for the given date.
|
|
710
|
+
* The result will be in the local timezone.
|
|
711
|
+
*
|
|
712
|
+
* @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
|
|
713
|
+
*
|
|
714
|
+
* @param date - The original date
|
|
715
|
+
*
|
|
716
|
+
* @returns The start of a year
|
|
717
|
+
*
|
|
718
|
+
* @example
|
|
719
|
+
* // The start of a year for 2 September 2014 11:55:00:
|
|
720
|
+
* const result = startOfYear(new Date(2014, 8, 2, 11, 55, 00))
|
|
721
|
+
* //=> Wed Jan 01 2014 00:00:00
|
|
722
|
+
*/
|
|
723
|
+
function startOfYear(date) {
|
|
724
|
+
const cleanDate = toDate(date);
|
|
725
|
+
const _date = constructFrom(date, 0);
|
|
726
|
+
_date.setFullYear(cleanDate.getFullYear(), 0, 1);
|
|
727
|
+
_date.setHours(0, 0, 0, 0);
|
|
728
|
+
return _date;
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
/**
|
|
732
|
+
* The {@link endOfWeek} function options.
|
|
733
|
+
*/
|
|
734
|
+
|
|
735
|
+
/**
|
|
736
|
+
* @name endOfWeek
|
|
737
|
+
* @category Week Helpers
|
|
738
|
+
* @summary Return the end of a week for the given date.
|
|
739
|
+
*
|
|
740
|
+
* @description
|
|
741
|
+
* Return the end of a week for the given date.
|
|
742
|
+
* The result will be in the local timezone.
|
|
743
|
+
*
|
|
744
|
+
* @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
|
|
745
|
+
*
|
|
746
|
+
* @param date - The original date
|
|
747
|
+
* @param options - An object with options
|
|
748
|
+
*
|
|
749
|
+
* @returns The end of a week
|
|
750
|
+
*
|
|
751
|
+
* @example
|
|
752
|
+
* // The end of a week for 2 September 2014 11:55:00:
|
|
753
|
+
* const result = endOfWeek(new Date(2014, 8, 2, 11, 55, 0))
|
|
754
|
+
* //=> Sat Sep 06 2014 23:59:59.999
|
|
755
|
+
*
|
|
756
|
+
* @example
|
|
757
|
+
* // If the week starts on Monday, the end of the week for 2 September 2014 11:55:00:
|
|
758
|
+
* const result = endOfWeek(new Date(2014, 8, 2, 11, 55, 0), { weekStartsOn: 1 })
|
|
759
|
+
* //=> Sun Sep 07 2014 23:59:59.999
|
|
760
|
+
*/
|
|
761
|
+
function endOfWeek(date, options) {
|
|
762
|
+
const defaultOptions = getDefaultOptions();
|
|
763
|
+
const weekStartsOn =
|
|
764
|
+
options?.weekStartsOn ??
|
|
765
|
+
options?.locale?.options?.weekStartsOn ??
|
|
766
|
+
defaultOptions.weekStartsOn ??
|
|
767
|
+
defaultOptions.locale?.options?.weekStartsOn ??
|
|
768
|
+
0;
|
|
769
|
+
|
|
770
|
+
const _date = toDate(date);
|
|
771
|
+
const day = _date.getDay();
|
|
772
|
+
const diff = (day < weekStartsOn ? -7 : 0) + 6 - (day - weekStartsOn);
|
|
773
|
+
|
|
774
|
+
_date.setDate(_date.getDate() + diff);
|
|
775
|
+
_date.setHours(23, 59, 59, 999);
|
|
776
|
+
return _date;
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
const formatDistanceLocale = {
|
|
780
|
+
lessThanXSeconds: {
|
|
781
|
+
one: "less than a second",
|
|
782
|
+
other: "less than {{count}} seconds",
|
|
783
|
+
},
|
|
784
|
+
|
|
785
|
+
xSeconds: {
|
|
786
|
+
one: "1 second",
|
|
787
|
+
other: "{{count}} seconds",
|
|
788
|
+
},
|
|
789
|
+
|
|
790
|
+
halfAMinute: "half a minute",
|
|
791
|
+
|
|
792
|
+
lessThanXMinutes: {
|
|
793
|
+
one: "less than a minute",
|
|
794
|
+
other: "less than {{count}} minutes",
|
|
795
|
+
},
|
|
796
|
+
|
|
797
|
+
xMinutes: {
|
|
798
|
+
one: "1 minute",
|
|
799
|
+
other: "{{count}} minutes",
|
|
800
|
+
},
|
|
801
|
+
|
|
802
|
+
aboutXHours: {
|
|
803
|
+
one: "about 1 hour",
|
|
804
|
+
other: "about {{count}} hours",
|
|
805
|
+
},
|
|
806
|
+
|
|
807
|
+
xHours: {
|
|
808
|
+
one: "1 hour",
|
|
809
|
+
other: "{{count}} hours",
|
|
810
|
+
},
|
|
811
|
+
|
|
812
|
+
xDays: {
|
|
813
|
+
one: "1 day",
|
|
814
|
+
other: "{{count}} days",
|
|
815
|
+
},
|
|
816
|
+
|
|
817
|
+
aboutXWeeks: {
|
|
818
|
+
one: "about 1 week",
|
|
819
|
+
other: "about {{count}} weeks",
|
|
820
|
+
},
|
|
821
|
+
|
|
822
|
+
xWeeks: {
|
|
823
|
+
one: "1 week",
|
|
824
|
+
other: "{{count}} weeks",
|
|
825
|
+
},
|
|
826
|
+
|
|
827
|
+
aboutXMonths: {
|
|
828
|
+
one: "about 1 month",
|
|
829
|
+
other: "about {{count}} months",
|
|
830
|
+
},
|
|
831
|
+
|
|
832
|
+
xMonths: {
|
|
833
|
+
one: "1 month",
|
|
834
|
+
other: "{{count}} months",
|
|
835
|
+
},
|
|
836
|
+
|
|
837
|
+
aboutXYears: {
|
|
838
|
+
one: "about 1 year",
|
|
839
|
+
other: "about {{count}} years",
|
|
840
|
+
},
|
|
841
|
+
|
|
842
|
+
xYears: {
|
|
843
|
+
one: "1 year",
|
|
844
|
+
other: "{{count}} years",
|
|
845
|
+
},
|
|
846
|
+
|
|
847
|
+
overXYears: {
|
|
848
|
+
one: "over 1 year",
|
|
849
|
+
other: "over {{count}} years",
|
|
850
|
+
},
|
|
851
|
+
|
|
852
|
+
almostXYears: {
|
|
853
|
+
one: "almost 1 year",
|
|
854
|
+
other: "almost {{count}} years",
|
|
855
|
+
},
|
|
856
|
+
};
|
|
857
|
+
|
|
858
|
+
const formatDistance = (token, count, options) => {
|
|
859
|
+
let result;
|
|
860
|
+
|
|
861
|
+
const tokenValue = formatDistanceLocale[token];
|
|
862
|
+
if (typeof tokenValue === "string") {
|
|
863
|
+
result = tokenValue;
|
|
864
|
+
} else if (count === 1) {
|
|
865
|
+
result = tokenValue.one;
|
|
866
|
+
} else {
|
|
867
|
+
result = tokenValue.other.replace("{{count}}", count.toString());
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
if (options?.addSuffix) {
|
|
871
|
+
if (options.comparison && options.comparison > 0) {
|
|
872
|
+
return "in " + result;
|
|
873
|
+
} else {
|
|
874
|
+
return result + " ago";
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
return result;
|
|
879
|
+
};
|
|
880
|
+
|
|
881
|
+
function buildFormatLongFn(args) {
|
|
882
|
+
return (options = {}) => {
|
|
883
|
+
// TODO: Remove String()
|
|
884
|
+
const width = options.width ? String(options.width) : args.defaultWidth;
|
|
885
|
+
const format = args.formats[width] || args.formats[args.defaultWidth];
|
|
886
|
+
return format;
|
|
887
|
+
};
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
const dateFormats = {
|
|
891
|
+
full: "EEEE, MMMM do, y",
|
|
892
|
+
long: "MMMM do, y",
|
|
893
|
+
medium: "MMM d, y",
|
|
894
|
+
short: "MM/dd/yyyy",
|
|
895
|
+
};
|
|
896
|
+
|
|
897
|
+
const timeFormats = {
|
|
898
|
+
full: "h:mm:ss a zzzz",
|
|
899
|
+
long: "h:mm:ss a z",
|
|
900
|
+
medium: "h:mm:ss a",
|
|
901
|
+
short: "h:mm a",
|
|
902
|
+
};
|
|
903
|
+
|
|
904
|
+
const dateTimeFormats = {
|
|
905
|
+
full: "{{date}} 'at' {{time}}",
|
|
906
|
+
long: "{{date}} 'at' {{time}}",
|
|
907
|
+
medium: "{{date}}, {{time}}",
|
|
908
|
+
short: "{{date}}, {{time}}",
|
|
909
|
+
};
|
|
910
|
+
|
|
911
|
+
const formatLong = {
|
|
912
|
+
date: buildFormatLongFn({
|
|
913
|
+
formats: dateFormats,
|
|
914
|
+
defaultWidth: "full",
|
|
915
|
+
}),
|
|
916
|
+
|
|
917
|
+
time: buildFormatLongFn({
|
|
918
|
+
formats: timeFormats,
|
|
919
|
+
defaultWidth: "full",
|
|
920
|
+
}),
|
|
921
|
+
|
|
922
|
+
dateTime: buildFormatLongFn({
|
|
923
|
+
formats: dateTimeFormats,
|
|
924
|
+
defaultWidth: "full",
|
|
925
|
+
}),
|
|
926
|
+
};
|
|
927
|
+
|
|
928
|
+
const formatRelativeLocale = {
|
|
929
|
+
lastWeek: "'last' eeee 'at' p",
|
|
930
|
+
yesterday: "'yesterday at' p",
|
|
931
|
+
today: "'today at' p",
|
|
932
|
+
tomorrow: "'tomorrow at' p",
|
|
933
|
+
nextWeek: "eeee 'at' p",
|
|
934
|
+
other: "P",
|
|
935
|
+
};
|
|
936
|
+
|
|
937
|
+
const formatRelative = (token, _date, _baseDate, _options) =>
|
|
938
|
+
formatRelativeLocale[token];
|
|
939
|
+
|
|
940
|
+
/* eslint-disable no-unused-vars */
|
|
941
|
+
|
|
942
|
+
/**
|
|
943
|
+
* The localize function argument callback which allows to convert raw value to
|
|
944
|
+
* the actual type.
|
|
945
|
+
*
|
|
946
|
+
* @param value - The value to convert
|
|
947
|
+
*
|
|
948
|
+
* @returns The converted value
|
|
949
|
+
*/
|
|
950
|
+
|
|
951
|
+
/**
|
|
952
|
+
* The map of localized values for each width.
|
|
953
|
+
*/
|
|
954
|
+
|
|
955
|
+
/**
|
|
956
|
+
* The index type of the locale unit value. It types conversion of units of
|
|
957
|
+
* values that don't start at 0 (i.e. quarters).
|
|
958
|
+
*/
|
|
959
|
+
|
|
960
|
+
/**
|
|
961
|
+
* Converts the unit value to the tuple of values.
|
|
962
|
+
*/
|
|
963
|
+
|
|
964
|
+
/**
|
|
965
|
+
* The tuple of localized era values. The first element represents BC,
|
|
966
|
+
* the second element represents AD.
|
|
967
|
+
*/
|
|
968
|
+
|
|
969
|
+
/**
|
|
970
|
+
* The tuple of localized quarter values. The first element represents Q1.
|
|
971
|
+
*/
|
|
972
|
+
|
|
973
|
+
/**
|
|
974
|
+
* The tuple of localized day values. The first element represents Sunday.
|
|
975
|
+
*/
|
|
976
|
+
|
|
977
|
+
/**
|
|
978
|
+
* The tuple of localized month values. The first element represents January.
|
|
979
|
+
*/
|
|
980
|
+
|
|
981
|
+
function buildLocalizeFn(args) {
|
|
982
|
+
return (value, options) => {
|
|
983
|
+
const context = options?.context ? String(options.context) : "standalone";
|
|
984
|
+
|
|
985
|
+
let valuesArray;
|
|
986
|
+
if (context === "formatting" && args.formattingValues) {
|
|
987
|
+
const defaultWidth = args.defaultFormattingWidth || args.defaultWidth;
|
|
988
|
+
const width = options?.width ? String(options.width) : defaultWidth;
|
|
989
|
+
|
|
990
|
+
valuesArray =
|
|
991
|
+
args.formattingValues[width] || args.formattingValues[defaultWidth];
|
|
992
|
+
} else {
|
|
993
|
+
const defaultWidth = args.defaultWidth;
|
|
994
|
+
const width = options?.width ? String(options.width) : args.defaultWidth;
|
|
995
|
+
|
|
996
|
+
valuesArray = args.values[width] || args.values[defaultWidth];
|
|
997
|
+
}
|
|
998
|
+
const index = args.argumentCallback ? args.argumentCallback(value) : value;
|
|
999
|
+
|
|
1000
|
+
// @ts-expect-error - For some reason TypeScript just don't want to match it, no matter how hard we try. I challenge you to try to remove it!
|
|
1001
|
+
return valuesArray[index];
|
|
1002
|
+
};
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
const eraValues = {
|
|
1006
|
+
narrow: ["B", "A"],
|
|
1007
|
+
abbreviated: ["BC", "AD"],
|
|
1008
|
+
wide: ["Before Christ", "Anno Domini"],
|
|
1009
|
+
};
|
|
1010
|
+
|
|
1011
|
+
const quarterValues = {
|
|
1012
|
+
narrow: ["1", "2", "3", "4"],
|
|
1013
|
+
abbreviated: ["Q1", "Q2", "Q3", "Q4"],
|
|
1014
|
+
wide: ["1st quarter", "2nd quarter", "3rd quarter", "4th quarter"],
|
|
1015
|
+
};
|
|
1016
|
+
|
|
1017
|
+
// Note: in English, the names of days of the week and months are capitalized.
|
|
1018
|
+
// If you are making a new locale based on this one, check if the same is true for the language you're working on.
|
|
1019
|
+
// Generally, formatted dates should look like they are in the middle of a sentence,
|
|
1020
|
+
// e.g. in Spanish language the weekdays and months should be in the lowercase.
|
|
1021
|
+
const monthValues = {
|
|
1022
|
+
narrow: ["J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D"],
|
|
1023
|
+
abbreviated: [
|
|
1024
|
+
"Jan",
|
|
1025
|
+
"Feb",
|
|
1026
|
+
"Mar",
|
|
1027
|
+
"Apr",
|
|
1028
|
+
"May",
|
|
1029
|
+
"Jun",
|
|
1030
|
+
"Jul",
|
|
1031
|
+
"Aug",
|
|
1032
|
+
"Sep",
|
|
1033
|
+
"Oct",
|
|
1034
|
+
"Nov",
|
|
1035
|
+
"Dec",
|
|
1036
|
+
],
|
|
1037
|
+
|
|
1038
|
+
wide: [
|
|
1039
|
+
"January",
|
|
1040
|
+
"February",
|
|
1041
|
+
"March",
|
|
1042
|
+
"April",
|
|
1043
|
+
"May",
|
|
1044
|
+
"June",
|
|
1045
|
+
"July",
|
|
1046
|
+
"August",
|
|
1047
|
+
"September",
|
|
1048
|
+
"October",
|
|
1049
|
+
"November",
|
|
1050
|
+
"December",
|
|
1051
|
+
],
|
|
1052
|
+
};
|
|
1053
|
+
|
|
1054
|
+
const dayValues = {
|
|
1055
|
+
narrow: ["S", "M", "T", "W", "T", "F", "S"],
|
|
1056
|
+
short: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"],
|
|
1057
|
+
abbreviated: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
|
|
1058
|
+
wide: [
|
|
1059
|
+
"Sunday",
|
|
1060
|
+
"Monday",
|
|
1061
|
+
"Tuesday",
|
|
1062
|
+
"Wednesday",
|
|
1063
|
+
"Thursday",
|
|
1064
|
+
"Friday",
|
|
1065
|
+
"Saturday",
|
|
1066
|
+
],
|
|
1067
|
+
};
|
|
1068
|
+
|
|
1069
|
+
const dayPeriodValues = {
|
|
1070
|
+
narrow: {
|
|
1071
|
+
am: "a",
|
|
1072
|
+
pm: "p",
|
|
1073
|
+
midnight: "mi",
|
|
1074
|
+
noon: "n",
|
|
1075
|
+
morning: "morning",
|
|
1076
|
+
afternoon: "afternoon",
|
|
1077
|
+
evening: "evening",
|
|
1078
|
+
night: "night",
|
|
1079
|
+
},
|
|
1080
|
+
abbreviated: {
|
|
1081
|
+
am: "AM",
|
|
1082
|
+
pm: "PM",
|
|
1083
|
+
midnight: "midnight",
|
|
1084
|
+
noon: "noon",
|
|
1085
|
+
morning: "morning",
|
|
1086
|
+
afternoon: "afternoon",
|
|
1087
|
+
evening: "evening",
|
|
1088
|
+
night: "night",
|
|
1089
|
+
},
|
|
1090
|
+
wide: {
|
|
1091
|
+
am: "a.m.",
|
|
1092
|
+
pm: "p.m.",
|
|
1093
|
+
midnight: "midnight",
|
|
1094
|
+
noon: "noon",
|
|
1095
|
+
morning: "morning",
|
|
1096
|
+
afternoon: "afternoon",
|
|
1097
|
+
evening: "evening",
|
|
1098
|
+
night: "night",
|
|
1099
|
+
},
|
|
1100
|
+
};
|
|
1101
|
+
|
|
1102
|
+
const formattingDayPeriodValues = {
|
|
1103
|
+
narrow: {
|
|
1104
|
+
am: "a",
|
|
1105
|
+
pm: "p",
|
|
1106
|
+
midnight: "mi",
|
|
1107
|
+
noon: "n",
|
|
1108
|
+
morning: "in the morning",
|
|
1109
|
+
afternoon: "in the afternoon",
|
|
1110
|
+
evening: "in the evening",
|
|
1111
|
+
night: "at night",
|
|
1112
|
+
},
|
|
1113
|
+
abbreviated: {
|
|
1114
|
+
am: "AM",
|
|
1115
|
+
pm: "PM",
|
|
1116
|
+
midnight: "midnight",
|
|
1117
|
+
noon: "noon",
|
|
1118
|
+
morning: "in the morning",
|
|
1119
|
+
afternoon: "in the afternoon",
|
|
1120
|
+
evening: "in the evening",
|
|
1121
|
+
night: "at night",
|
|
1122
|
+
},
|
|
1123
|
+
wide: {
|
|
1124
|
+
am: "a.m.",
|
|
1125
|
+
pm: "p.m.",
|
|
1126
|
+
midnight: "midnight",
|
|
1127
|
+
noon: "noon",
|
|
1128
|
+
morning: "in the morning",
|
|
1129
|
+
afternoon: "in the afternoon",
|
|
1130
|
+
evening: "in the evening",
|
|
1131
|
+
night: "at night",
|
|
1132
|
+
},
|
|
1133
|
+
};
|
|
1134
|
+
|
|
1135
|
+
const ordinalNumber = (dirtyNumber, _options) => {
|
|
1136
|
+
const number = Number(dirtyNumber);
|
|
1137
|
+
|
|
1138
|
+
// If ordinal numbers depend on context, for example,
|
|
1139
|
+
// if they are different for different grammatical genders,
|
|
1140
|
+
// use `options.unit`.
|
|
1141
|
+
//
|
|
1142
|
+
// `unit` can be 'year', 'quarter', 'month', 'week', 'date', 'dayOfYear',
|
|
1143
|
+
// 'day', 'hour', 'minute', 'second'.
|
|
1144
|
+
|
|
1145
|
+
const rem100 = number % 100;
|
|
1146
|
+
if (rem100 > 20 || rem100 < 10) {
|
|
1147
|
+
switch (rem100 % 10) {
|
|
1148
|
+
case 1:
|
|
1149
|
+
return number + "st";
|
|
1150
|
+
case 2:
|
|
1151
|
+
return number + "nd";
|
|
1152
|
+
case 3:
|
|
1153
|
+
return number + "rd";
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
return number + "th";
|
|
1157
|
+
};
|
|
1158
|
+
|
|
1159
|
+
const localize = {
|
|
1160
|
+
ordinalNumber,
|
|
1161
|
+
|
|
1162
|
+
era: buildLocalizeFn({
|
|
1163
|
+
values: eraValues,
|
|
1164
|
+
defaultWidth: "wide",
|
|
1165
|
+
}),
|
|
1166
|
+
|
|
1167
|
+
quarter: buildLocalizeFn({
|
|
1168
|
+
values: quarterValues,
|
|
1169
|
+
defaultWidth: "wide",
|
|
1170
|
+
argumentCallback: (quarter) => quarter - 1,
|
|
1171
|
+
}),
|
|
1172
|
+
|
|
1173
|
+
month: buildLocalizeFn({
|
|
1174
|
+
values: monthValues,
|
|
1175
|
+
defaultWidth: "wide",
|
|
1176
|
+
}),
|
|
1177
|
+
|
|
1178
|
+
day: buildLocalizeFn({
|
|
1179
|
+
values: dayValues,
|
|
1180
|
+
defaultWidth: "wide",
|
|
1181
|
+
}),
|
|
1182
|
+
|
|
1183
|
+
dayPeriod: buildLocalizeFn({
|
|
1184
|
+
values: dayPeriodValues,
|
|
1185
|
+
defaultWidth: "wide",
|
|
1186
|
+
formattingValues: formattingDayPeriodValues,
|
|
1187
|
+
defaultFormattingWidth: "wide",
|
|
1188
|
+
}),
|
|
1189
|
+
};
|
|
1190
|
+
|
|
1191
|
+
function buildMatchFn(args) {
|
|
1192
|
+
return (string, options = {}) => {
|
|
1193
|
+
const width = options.width;
|
|
1194
|
+
|
|
1195
|
+
const matchPattern =
|
|
1196
|
+
(width && args.matchPatterns[width]) ||
|
|
1197
|
+
args.matchPatterns[args.defaultMatchWidth];
|
|
1198
|
+
const matchResult = string.match(matchPattern);
|
|
1199
|
+
|
|
1200
|
+
if (!matchResult) {
|
|
1201
|
+
return null;
|
|
1202
|
+
}
|
|
1203
|
+
const matchedString = matchResult[0];
|
|
1204
|
+
|
|
1205
|
+
const parsePatterns =
|
|
1206
|
+
(width && args.parsePatterns[width]) ||
|
|
1207
|
+
args.parsePatterns[args.defaultParseWidth];
|
|
1208
|
+
|
|
1209
|
+
const key = Array.isArray(parsePatterns)
|
|
1210
|
+
? findIndex(parsePatterns, (pattern) => pattern.test(matchedString))
|
|
1211
|
+
: // eslint-disable-next-line @typescript-eslint/no-explicit-any -- I challange you to fix the type
|
|
1212
|
+
findKey(parsePatterns, (pattern) => pattern.test(matchedString));
|
|
1213
|
+
|
|
1214
|
+
let value;
|
|
1215
|
+
|
|
1216
|
+
value = args.valueCallback ? args.valueCallback(key) : key;
|
|
1217
|
+
value = options.valueCallback
|
|
1218
|
+
? // eslint-disable-next-line @typescript-eslint/no-explicit-any -- I challange you to fix the type
|
|
1219
|
+
options.valueCallback(value)
|
|
1220
|
+
: value;
|
|
1221
|
+
|
|
1222
|
+
const rest = string.slice(matchedString.length);
|
|
1223
|
+
|
|
1224
|
+
return { value, rest };
|
|
1225
|
+
};
|
|
1226
|
+
}
|
|
1227
|
+
|
|
1228
|
+
function findKey(object, predicate) {
|
|
1229
|
+
for (const key in object) {
|
|
1230
|
+
if (
|
|
1231
|
+
Object.prototype.hasOwnProperty.call(object, key) &&
|
|
1232
|
+
predicate(object[key])
|
|
1233
|
+
) {
|
|
1234
|
+
return key;
|
|
1235
|
+
}
|
|
1236
|
+
}
|
|
1237
|
+
return undefined;
|
|
1238
|
+
}
|
|
1239
|
+
|
|
1240
|
+
function findIndex(array, predicate) {
|
|
1241
|
+
for (let key = 0; key < array.length; key++) {
|
|
1242
|
+
if (predicate(array[key])) {
|
|
1243
|
+
return key;
|
|
1244
|
+
}
|
|
1245
|
+
}
|
|
1246
|
+
return undefined;
|
|
1247
|
+
}
|
|
1248
|
+
|
|
1249
|
+
function buildMatchPatternFn(args) {
|
|
1250
|
+
return (string, options = {}) => {
|
|
1251
|
+
const matchResult = string.match(args.matchPattern);
|
|
1252
|
+
if (!matchResult) return null;
|
|
1253
|
+
const matchedString = matchResult[0];
|
|
1254
|
+
|
|
1255
|
+
const parseResult = string.match(args.parsePattern);
|
|
1256
|
+
if (!parseResult) return null;
|
|
1257
|
+
let value = args.valueCallback
|
|
1258
|
+
? args.valueCallback(parseResult[0])
|
|
1259
|
+
: parseResult[0];
|
|
1260
|
+
|
|
1261
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- I challange you to fix the type
|
|
1262
|
+
value = options.valueCallback ? options.valueCallback(value) : value;
|
|
1263
|
+
|
|
1264
|
+
const rest = string.slice(matchedString.length);
|
|
1265
|
+
|
|
1266
|
+
return { value, rest };
|
|
1267
|
+
};
|
|
1268
|
+
}
|
|
1269
|
+
|
|
1270
|
+
const matchOrdinalNumberPattern = /^(\d+)(th|st|nd|rd)?/i;
|
|
1271
|
+
const parseOrdinalNumberPattern = /\d+/i;
|
|
1272
|
+
|
|
1273
|
+
const matchEraPatterns = {
|
|
1274
|
+
narrow: /^(b|a)/i,
|
|
1275
|
+
abbreviated: /^(b\.?\s?c\.?|b\.?\s?c\.?\s?e\.?|a\.?\s?d\.?|c\.?\s?e\.?)/i,
|
|
1276
|
+
wide: /^(before christ|before common era|anno domini|common era)/i,
|
|
1277
|
+
};
|
|
1278
|
+
const parseEraPatterns = {
|
|
1279
|
+
any: [/^b/i, /^(a|c)/i],
|
|
1280
|
+
};
|
|
1281
|
+
|
|
1282
|
+
const matchQuarterPatterns = {
|
|
1283
|
+
narrow: /^[1234]/i,
|
|
1284
|
+
abbreviated: /^q[1234]/i,
|
|
1285
|
+
wide: /^[1234](th|st|nd|rd)? quarter/i,
|
|
1286
|
+
};
|
|
1287
|
+
const parseQuarterPatterns = {
|
|
1288
|
+
any: [/1/i, /2/i, /3/i, /4/i],
|
|
1289
|
+
};
|
|
1290
|
+
|
|
1291
|
+
const matchMonthPatterns = {
|
|
1292
|
+
narrow: /^[jfmasond]/i,
|
|
1293
|
+
abbreviated: /^(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)/i,
|
|
1294
|
+
wide: /^(january|february|march|april|may|june|july|august|september|october|november|december)/i,
|
|
1295
|
+
};
|
|
1296
|
+
const parseMonthPatterns = {
|
|
1297
|
+
narrow: [
|
|
1298
|
+
/^j/i,
|
|
1299
|
+
/^f/i,
|
|
1300
|
+
/^m/i,
|
|
1301
|
+
/^a/i,
|
|
1302
|
+
/^m/i,
|
|
1303
|
+
/^j/i,
|
|
1304
|
+
/^j/i,
|
|
1305
|
+
/^a/i,
|
|
1306
|
+
/^s/i,
|
|
1307
|
+
/^o/i,
|
|
1308
|
+
/^n/i,
|
|
1309
|
+
/^d/i,
|
|
1310
|
+
],
|
|
1311
|
+
|
|
1312
|
+
any: [
|
|
1313
|
+
/^ja/i,
|
|
1314
|
+
/^f/i,
|
|
1315
|
+
/^mar/i,
|
|
1316
|
+
/^ap/i,
|
|
1317
|
+
/^may/i,
|
|
1318
|
+
/^jun/i,
|
|
1319
|
+
/^jul/i,
|
|
1320
|
+
/^au/i,
|
|
1321
|
+
/^s/i,
|
|
1322
|
+
/^o/i,
|
|
1323
|
+
/^n/i,
|
|
1324
|
+
/^d/i,
|
|
1325
|
+
],
|
|
1326
|
+
};
|
|
1327
|
+
|
|
1328
|
+
const matchDayPatterns = {
|
|
1329
|
+
narrow: /^[smtwf]/i,
|
|
1330
|
+
short: /^(su|mo|tu|we|th|fr|sa)/i,
|
|
1331
|
+
abbreviated: /^(sun|mon|tue|wed|thu|fri|sat)/i,
|
|
1332
|
+
wide: /^(sunday|monday|tuesday|wednesday|thursday|friday|saturday)/i,
|
|
1333
|
+
};
|
|
1334
|
+
const parseDayPatterns = {
|
|
1335
|
+
narrow: [/^s/i, /^m/i, /^t/i, /^w/i, /^t/i, /^f/i, /^s/i],
|
|
1336
|
+
any: [/^su/i, /^m/i, /^tu/i, /^w/i, /^th/i, /^f/i, /^sa/i],
|
|
1337
|
+
};
|
|
1338
|
+
|
|
1339
|
+
const matchDayPeriodPatterns = {
|
|
1340
|
+
narrow: /^(a|p|mi|n|(in the|at) (morning|afternoon|evening|night))/i,
|
|
1341
|
+
any: /^([ap]\.?\s?m\.?|midnight|noon|(in the|at) (morning|afternoon|evening|night))/i,
|
|
1342
|
+
};
|
|
1343
|
+
const parseDayPeriodPatterns = {
|
|
1344
|
+
any: {
|
|
1345
|
+
am: /^a/i,
|
|
1346
|
+
pm: /^p/i,
|
|
1347
|
+
midnight: /^mi/i,
|
|
1348
|
+
noon: /^no/i,
|
|
1349
|
+
morning: /morning/i,
|
|
1350
|
+
afternoon: /afternoon/i,
|
|
1351
|
+
evening: /evening/i,
|
|
1352
|
+
night: /night/i,
|
|
1353
|
+
},
|
|
1354
|
+
};
|
|
1355
|
+
|
|
1356
|
+
const match = {
|
|
1357
|
+
ordinalNumber: buildMatchPatternFn({
|
|
1358
|
+
matchPattern: matchOrdinalNumberPattern,
|
|
1359
|
+
parsePattern: parseOrdinalNumberPattern,
|
|
1360
|
+
valueCallback: (value) => parseInt(value, 10),
|
|
1361
|
+
}),
|
|
1362
|
+
|
|
1363
|
+
era: buildMatchFn({
|
|
1364
|
+
matchPatterns: matchEraPatterns,
|
|
1365
|
+
defaultMatchWidth: "wide",
|
|
1366
|
+
parsePatterns: parseEraPatterns,
|
|
1367
|
+
defaultParseWidth: "any",
|
|
1368
|
+
}),
|
|
1369
|
+
|
|
1370
|
+
quarter: buildMatchFn({
|
|
1371
|
+
matchPatterns: matchQuarterPatterns,
|
|
1372
|
+
defaultMatchWidth: "wide",
|
|
1373
|
+
parsePatterns: parseQuarterPatterns,
|
|
1374
|
+
defaultParseWidth: "any",
|
|
1375
|
+
valueCallback: (index) => index + 1,
|
|
1376
|
+
}),
|
|
1377
|
+
|
|
1378
|
+
month: buildMatchFn({
|
|
1379
|
+
matchPatterns: matchMonthPatterns,
|
|
1380
|
+
defaultMatchWidth: "wide",
|
|
1381
|
+
parsePatterns: parseMonthPatterns,
|
|
1382
|
+
defaultParseWidth: "any",
|
|
1383
|
+
}),
|
|
1384
|
+
|
|
1385
|
+
day: buildMatchFn({
|
|
1386
|
+
matchPatterns: matchDayPatterns,
|
|
1387
|
+
defaultMatchWidth: "wide",
|
|
1388
|
+
parsePatterns: parseDayPatterns,
|
|
1389
|
+
defaultParseWidth: "any",
|
|
1390
|
+
}),
|
|
1391
|
+
|
|
1392
|
+
dayPeriod: buildMatchFn({
|
|
1393
|
+
matchPatterns: matchDayPeriodPatterns,
|
|
1394
|
+
defaultMatchWidth: "any",
|
|
1395
|
+
parsePatterns: parseDayPeriodPatterns,
|
|
1396
|
+
defaultParseWidth: "any",
|
|
1397
|
+
}),
|
|
1398
|
+
};
|
|
1399
|
+
|
|
1400
|
+
/**
|
|
1401
|
+
* @category Locales
|
|
1402
|
+
* @summary English locale (United States).
|
|
1403
|
+
* @language English
|
|
1404
|
+
* @iso-639-2 eng
|
|
1405
|
+
* @author Sasha Koss [@kossnocorp](https://github.com/kossnocorp)
|
|
1406
|
+
* @author Lesha Koss [@leshakoss](https://github.com/leshakoss)
|
|
1407
|
+
*/
|
|
1408
|
+
const enUS = {
|
|
1409
|
+
code: "en-US",
|
|
1410
|
+
formatDistance: formatDistance,
|
|
1411
|
+
formatLong: formatLong,
|
|
1412
|
+
formatRelative: formatRelative,
|
|
1413
|
+
localize: localize,
|
|
1414
|
+
match: match,
|
|
1415
|
+
options: {
|
|
1416
|
+
weekStartsOn: 0 /* Sunday */,
|
|
1417
|
+
firstWeekContainsDate: 1,
|
|
1418
|
+
},
|
|
1419
|
+
};
|
|
1420
|
+
|
|
1421
|
+
/**
|
|
1422
|
+
* @name getDayOfYear
|
|
1423
|
+
* @category Day Helpers
|
|
1424
|
+
* @summary Get the day of the year of the given date.
|
|
1425
|
+
*
|
|
1426
|
+
* @description
|
|
1427
|
+
* Get the day of the year of the given date.
|
|
1428
|
+
*
|
|
1429
|
+
* @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
|
|
1430
|
+
*
|
|
1431
|
+
* @param date - The given date
|
|
1432
|
+
*
|
|
1433
|
+
* @returns The day of year
|
|
1434
|
+
*
|
|
1435
|
+
* @example
|
|
1436
|
+
* // Which day of the year is 2 July 2014?
|
|
1437
|
+
* const result = getDayOfYear(new Date(2014, 6, 2))
|
|
1438
|
+
* //=> 183
|
|
1439
|
+
*/
|
|
1440
|
+
function getDayOfYear(date) {
|
|
1441
|
+
const _date = toDate(date);
|
|
1442
|
+
const diff = differenceInCalendarDays(_date, startOfYear(_date));
|
|
1443
|
+
const dayOfYear = diff + 1;
|
|
1444
|
+
return dayOfYear;
|
|
1445
|
+
}
|
|
1446
|
+
|
|
1447
|
+
/**
|
|
1448
|
+
* @name getISOWeek
|
|
1449
|
+
* @category ISO Week Helpers
|
|
1450
|
+
* @summary Get the ISO week of the given date.
|
|
1451
|
+
*
|
|
1452
|
+
* @description
|
|
1453
|
+
* Get the ISO week of the given date.
|
|
1454
|
+
*
|
|
1455
|
+
* ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
|
|
1456
|
+
*
|
|
1457
|
+
* @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
|
|
1458
|
+
*
|
|
1459
|
+
* @param date - The given date
|
|
1460
|
+
*
|
|
1461
|
+
* @returns The ISO week
|
|
1462
|
+
*
|
|
1463
|
+
* @example
|
|
1464
|
+
* // Which week of the ISO-week numbering year is 2 January 2005?
|
|
1465
|
+
* const result = getISOWeek(new Date(2005, 0, 2))
|
|
1466
|
+
* //=> 53
|
|
1467
|
+
*/
|
|
1468
|
+
function getISOWeek(date) {
|
|
1469
|
+
const _date = toDate(date);
|
|
1470
|
+
const diff = +startOfISOWeek(_date) - +startOfISOWeekYear(_date);
|
|
1471
|
+
|
|
1472
|
+
// Round the number of weeks to the nearest integer because the number of
|
|
1473
|
+
// milliseconds in a week is not constant (e.g. it's different in the week of
|
|
1474
|
+
// the daylight saving time clock shift).
|
|
1475
|
+
return Math.round(diff / millisecondsInWeek) + 1;
|
|
1476
|
+
}
|
|
1477
|
+
|
|
1478
|
+
/**
|
|
1479
|
+
* The {@link getWeekYear} function options.
|
|
1480
|
+
*/
|
|
1481
|
+
|
|
1482
|
+
/**
|
|
1483
|
+
* @name getWeekYear
|
|
1484
|
+
* @category Week-Numbering Year Helpers
|
|
1485
|
+
* @summary Get the local week-numbering year of the given date.
|
|
1486
|
+
*
|
|
1487
|
+
* @description
|
|
1488
|
+
* Get the local week-numbering year of the given date.
|
|
1489
|
+
* The exact calculation depends on the values of
|
|
1490
|
+
* `options.weekStartsOn` (which is the index of the first day of the week)
|
|
1491
|
+
* and `options.firstWeekContainsDate` (which is the day of January, which is always in
|
|
1492
|
+
* the first week of the week-numbering year)
|
|
1493
|
+
*
|
|
1494
|
+
* Week numbering: https://en.wikipedia.org/wiki/Week#The_ISO_week_date_system
|
|
1495
|
+
*
|
|
1496
|
+
* @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
|
|
1497
|
+
*
|
|
1498
|
+
* @param date - The given date
|
|
1499
|
+
* @param options - An object with options.
|
|
1500
|
+
*
|
|
1501
|
+
* @returns The local week-numbering year
|
|
1502
|
+
*
|
|
1503
|
+
* @example
|
|
1504
|
+
* // Which week numbering year is 26 December 2004 with the default settings?
|
|
1505
|
+
* const result = getWeekYear(new Date(2004, 11, 26))
|
|
1506
|
+
* //=> 2005
|
|
1507
|
+
*
|
|
1508
|
+
* @example
|
|
1509
|
+
* // Which week numbering year is 26 December 2004 if week starts on Saturday?
|
|
1510
|
+
* const result = getWeekYear(new Date(2004, 11, 26), { weekStartsOn: 6 })
|
|
1511
|
+
* //=> 2004
|
|
1512
|
+
*
|
|
1513
|
+
* @example
|
|
1514
|
+
* // Which week numbering year is 26 December 2004 if the first week contains 4 January?
|
|
1515
|
+
* const result = getWeekYear(new Date(2004, 11, 26), { firstWeekContainsDate: 4 })
|
|
1516
|
+
* //=> 2004
|
|
1517
|
+
*/
|
|
1518
|
+
function getWeekYear(date, options) {
|
|
1519
|
+
const _date = toDate(date);
|
|
1520
|
+
const year = _date.getFullYear();
|
|
1521
|
+
|
|
1522
|
+
const defaultOptions = getDefaultOptions();
|
|
1523
|
+
const firstWeekContainsDate =
|
|
1524
|
+
options?.firstWeekContainsDate ??
|
|
1525
|
+
options?.locale?.options?.firstWeekContainsDate ??
|
|
1526
|
+
defaultOptions.firstWeekContainsDate ??
|
|
1527
|
+
defaultOptions.locale?.options?.firstWeekContainsDate ??
|
|
1528
|
+
1;
|
|
1529
|
+
|
|
1530
|
+
const firstWeekOfNextYear = constructFrom(date, 0);
|
|
1531
|
+
firstWeekOfNextYear.setFullYear(year + 1, 0, firstWeekContainsDate);
|
|
1532
|
+
firstWeekOfNextYear.setHours(0, 0, 0, 0);
|
|
1533
|
+
const startOfNextYear = startOfWeek(firstWeekOfNextYear, options);
|
|
1534
|
+
|
|
1535
|
+
const firstWeekOfThisYear = constructFrom(date, 0);
|
|
1536
|
+
firstWeekOfThisYear.setFullYear(year, 0, firstWeekContainsDate);
|
|
1537
|
+
firstWeekOfThisYear.setHours(0, 0, 0, 0);
|
|
1538
|
+
const startOfThisYear = startOfWeek(firstWeekOfThisYear, options);
|
|
1539
|
+
|
|
1540
|
+
if (_date.getTime() >= startOfNextYear.getTime()) {
|
|
1541
|
+
return year + 1;
|
|
1542
|
+
} else if (_date.getTime() >= startOfThisYear.getTime()) {
|
|
1543
|
+
return year;
|
|
1544
|
+
} else {
|
|
1545
|
+
return year - 1;
|
|
1546
|
+
}
|
|
1547
|
+
}
|
|
1548
|
+
|
|
1549
|
+
/**
|
|
1550
|
+
* The {@link startOfWeekYear} function options.
|
|
1551
|
+
*/
|
|
1552
|
+
|
|
1553
|
+
/**
|
|
1554
|
+
* @name startOfWeekYear
|
|
1555
|
+
* @category Week-Numbering Year Helpers
|
|
1556
|
+
* @summary Return the start of a local week-numbering year for the given date.
|
|
1557
|
+
*
|
|
1558
|
+
* @description
|
|
1559
|
+
* Return the start of a local week-numbering year.
|
|
1560
|
+
* The exact calculation depends on the values of
|
|
1561
|
+
* `options.weekStartsOn` (which is the index of the first day of the week)
|
|
1562
|
+
* and `options.firstWeekContainsDate` (which is the day of January, which is always in
|
|
1563
|
+
* the first week of the week-numbering year)
|
|
1564
|
+
*
|
|
1565
|
+
* Week numbering: https://en.wikipedia.org/wiki/Week#The_ISO_week_date_system
|
|
1566
|
+
*
|
|
1567
|
+
* @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
|
|
1568
|
+
*
|
|
1569
|
+
* @param date - The original date
|
|
1570
|
+
* @param options - An object with options
|
|
1571
|
+
*
|
|
1572
|
+
* @returns The start of a week-numbering year
|
|
1573
|
+
*
|
|
1574
|
+
* @example
|
|
1575
|
+
* // The start of an a week-numbering year for 2 July 2005 with default settings:
|
|
1576
|
+
* const result = startOfWeekYear(new Date(2005, 6, 2))
|
|
1577
|
+
* //=> Sun Dec 26 2004 00:00:00
|
|
1578
|
+
*
|
|
1579
|
+
* @example
|
|
1580
|
+
* // The start of a week-numbering year for 2 July 2005
|
|
1581
|
+
* // if Monday is the first day of week
|
|
1582
|
+
* // and 4 January is always in the first week of the year:
|
|
1583
|
+
* const result = startOfWeekYear(new Date(2005, 6, 2), {
|
|
1584
|
+
* weekStartsOn: 1,
|
|
1585
|
+
* firstWeekContainsDate: 4
|
|
1586
|
+
* })
|
|
1587
|
+
* //=> Mon Jan 03 2005 00:00:00
|
|
1588
|
+
*/
|
|
1589
|
+
function startOfWeekYear(date, options) {
|
|
1590
|
+
const defaultOptions = getDefaultOptions();
|
|
1591
|
+
const firstWeekContainsDate =
|
|
1592
|
+
options?.firstWeekContainsDate ??
|
|
1593
|
+
options?.locale?.options?.firstWeekContainsDate ??
|
|
1594
|
+
defaultOptions.firstWeekContainsDate ??
|
|
1595
|
+
defaultOptions.locale?.options?.firstWeekContainsDate ??
|
|
1596
|
+
1;
|
|
1597
|
+
|
|
1598
|
+
const year = getWeekYear(date, options);
|
|
1599
|
+
const firstWeek = constructFrom(date, 0);
|
|
1600
|
+
firstWeek.setFullYear(year, 0, firstWeekContainsDate);
|
|
1601
|
+
firstWeek.setHours(0, 0, 0, 0);
|
|
1602
|
+
const _date = startOfWeek(firstWeek, options);
|
|
1603
|
+
return _date;
|
|
1604
|
+
}
|
|
1605
|
+
|
|
1606
|
+
/**
|
|
1607
|
+
* The {@link getWeek} function options.
|
|
1608
|
+
*/
|
|
1609
|
+
|
|
1610
|
+
/**
|
|
1611
|
+
* @name getWeek
|
|
1612
|
+
* @category Week Helpers
|
|
1613
|
+
* @summary Get the local week index of the given date.
|
|
1614
|
+
*
|
|
1615
|
+
* @description
|
|
1616
|
+
* Get the local week index of the given date.
|
|
1617
|
+
* The exact calculation depends on the values of
|
|
1618
|
+
* `options.weekStartsOn` (which is the index of the first day of the week)
|
|
1619
|
+
* and `options.firstWeekContainsDate` (which is the day of January, which is always in
|
|
1620
|
+
* the first week of the week-numbering year)
|
|
1621
|
+
*
|
|
1622
|
+
* Week numbering: https://en.wikipedia.org/wiki/Week#The_ISO_week_date_system
|
|
1623
|
+
*
|
|
1624
|
+
* @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
|
|
1625
|
+
*
|
|
1626
|
+
* @param date - The given date
|
|
1627
|
+
* @param options - An object with options
|
|
1628
|
+
*
|
|
1629
|
+
* @returns The week
|
|
1630
|
+
*
|
|
1631
|
+
* @example
|
|
1632
|
+
* // Which week of the local week numbering year is 2 January 2005 with default options?
|
|
1633
|
+
* const result = getWeek(new Date(2005, 0, 2))
|
|
1634
|
+
* //=> 2
|
|
1635
|
+
*
|
|
1636
|
+
* @example
|
|
1637
|
+
* // Which week of the local week numbering year is 2 January 2005,
|
|
1638
|
+
* // if Monday is the first day of the week,
|
|
1639
|
+
* // and the first week of the year always contains 4 January?
|
|
1640
|
+
* const result = getWeek(new Date(2005, 0, 2), {
|
|
1641
|
+
* weekStartsOn: 1,
|
|
1642
|
+
* firstWeekContainsDate: 4
|
|
1643
|
+
* })
|
|
1644
|
+
* //=> 53
|
|
1645
|
+
*/
|
|
1646
|
+
|
|
1647
|
+
function getWeek(date, options) {
|
|
1648
|
+
const _date = toDate(date);
|
|
1649
|
+
const diff = +startOfWeek(_date, options) - +startOfWeekYear(_date, options);
|
|
1650
|
+
|
|
1651
|
+
// Round the number of weeks to the nearest integer because the number of
|
|
1652
|
+
// milliseconds in a week is not constant (e.g. it's different in the week of
|
|
1653
|
+
// the daylight saving time clock shift).
|
|
1654
|
+
return Math.round(diff / millisecondsInWeek) + 1;
|
|
1655
|
+
}
|
|
1656
|
+
|
|
1657
|
+
function addLeadingZeros(number, targetLength) {
|
|
1658
|
+
const sign = number < 0 ? "-" : "";
|
|
1659
|
+
const output = Math.abs(number).toString().padStart(targetLength, "0");
|
|
1660
|
+
return sign + output;
|
|
1661
|
+
}
|
|
1662
|
+
|
|
1663
|
+
/*
|
|
1664
|
+
* | | Unit | | Unit |
|
|
1665
|
+
* |-----|--------------------------------|-----|--------------------------------|
|
|
1666
|
+
* | a | AM, PM | A* | |
|
|
1667
|
+
* | d | Day of month | D | |
|
|
1668
|
+
* | h | Hour [1-12] | H | Hour [0-23] |
|
|
1669
|
+
* | m | Minute | M | Month |
|
|
1670
|
+
* | s | Second | S | Fraction of second |
|
|
1671
|
+
* | y | Year (abs) | Y | |
|
|
1672
|
+
*
|
|
1673
|
+
* Letters marked by * are not implemented but reserved by Unicode standard.
|
|
1674
|
+
*/
|
|
1675
|
+
|
|
1676
|
+
const lightFormatters = {
|
|
1677
|
+
// Year
|
|
1678
|
+
y(date, token) {
|
|
1679
|
+
// From http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_tokens
|
|
1680
|
+
// | Year | y | yy | yyy | yyyy | yyyyy |
|
|
1681
|
+
// |----------|-------|----|-------|-------|-------|
|
|
1682
|
+
// | AD 1 | 1 | 01 | 001 | 0001 | 00001 |
|
|
1683
|
+
// | AD 12 | 12 | 12 | 012 | 0012 | 00012 |
|
|
1684
|
+
// | AD 123 | 123 | 23 | 123 | 0123 | 00123 |
|
|
1685
|
+
// | AD 1234 | 1234 | 34 | 1234 | 1234 | 01234 |
|
|
1686
|
+
// | AD 12345 | 12345 | 45 | 12345 | 12345 | 12345 |
|
|
1687
|
+
|
|
1688
|
+
const signedYear = date.getFullYear();
|
|
1689
|
+
// Returns 1 for 1 BC (which is year 0 in JavaScript)
|
|
1690
|
+
const year = signedYear > 0 ? signedYear : 1 - signedYear;
|
|
1691
|
+
return addLeadingZeros(token === "yy" ? year % 100 : year, token.length);
|
|
1692
|
+
},
|
|
1693
|
+
|
|
1694
|
+
// Month
|
|
1695
|
+
M(date, token) {
|
|
1696
|
+
const month = date.getMonth();
|
|
1697
|
+
return token === "M" ? String(month + 1) : addLeadingZeros(month + 1, 2);
|
|
1698
|
+
},
|
|
1699
|
+
|
|
1700
|
+
// Day of the month
|
|
1701
|
+
d(date, token) {
|
|
1702
|
+
return addLeadingZeros(date.getDate(), token.length);
|
|
1703
|
+
},
|
|
1704
|
+
|
|
1705
|
+
// AM or PM
|
|
1706
|
+
a(date, token) {
|
|
1707
|
+
const dayPeriodEnumValue = date.getHours() / 12 >= 1 ? "pm" : "am";
|
|
1708
|
+
|
|
1709
|
+
switch (token) {
|
|
1710
|
+
case "a":
|
|
1711
|
+
case "aa":
|
|
1712
|
+
return dayPeriodEnumValue.toUpperCase();
|
|
1713
|
+
case "aaa":
|
|
1714
|
+
return dayPeriodEnumValue;
|
|
1715
|
+
case "aaaaa":
|
|
1716
|
+
return dayPeriodEnumValue[0];
|
|
1717
|
+
case "aaaa":
|
|
1718
|
+
default:
|
|
1719
|
+
return dayPeriodEnumValue === "am" ? "a.m." : "p.m.";
|
|
1720
|
+
}
|
|
1721
|
+
},
|
|
1722
|
+
|
|
1723
|
+
// Hour [1-12]
|
|
1724
|
+
h(date, token) {
|
|
1725
|
+
return addLeadingZeros(date.getHours() % 12 || 12, token.length);
|
|
1726
|
+
},
|
|
1727
|
+
|
|
1728
|
+
// Hour [0-23]
|
|
1729
|
+
H(date, token) {
|
|
1730
|
+
return addLeadingZeros(date.getHours(), token.length);
|
|
1731
|
+
},
|
|
1732
|
+
|
|
1733
|
+
// Minute
|
|
1734
|
+
m(date, token) {
|
|
1735
|
+
return addLeadingZeros(date.getMinutes(), token.length);
|
|
1736
|
+
},
|
|
1737
|
+
|
|
1738
|
+
// Second
|
|
1739
|
+
s(date, token) {
|
|
1740
|
+
return addLeadingZeros(date.getSeconds(), token.length);
|
|
1741
|
+
},
|
|
1742
|
+
|
|
1743
|
+
// Fraction of second
|
|
1744
|
+
S(date, token) {
|
|
1745
|
+
const numberOfDigits = token.length;
|
|
1746
|
+
const milliseconds = date.getMilliseconds();
|
|
1747
|
+
const fractionalSeconds = Math.trunc(
|
|
1748
|
+
milliseconds * Math.pow(10, numberOfDigits - 3),
|
|
1749
|
+
);
|
|
1750
|
+
return addLeadingZeros(fractionalSeconds, token.length);
|
|
1751
|
+
},
|
|
1752
|
+
};
|
|
1753
|
+
|
|
1754
|
+
const dayPeriodEnum = {
|
|
1755
|
+
am: "am",
|
|
1756
|
+
pm: "pm",
|
|
1757
|
+
midnight: "midnight",
|
|
1758
|
+
noon: "noon",
|
|
1759
|
+
morning: "morning",
|
|
1760
|
+
afternoon: "afternoon",
|
|
1761
|
+
evening: "evening",
|
|
1762
|
+
night: "night",
|
|
1763
|
+
};
|
|
1764
|
+
|
|
1765
|
+
/*
|
|
1766
|
+
* | | Unit | | Unit |
|
|
1767
|
+
* |-----|--------------------------------|-----|--------------------------------|
|
|
1768
|
+
* | a | AM, PM | A* | Milliseconds in day |
|
|
1769
|
+
* | b | AM, PM, noon, midnight | B | Flexible day period |
|
|
1770
|
+
* | c | Stand-alone local day of week | C* | Localized hour w/ day period |
|
|
1771
|
+
* | d | Day of month | D | Day of year |
|
|
1772
|
+
* | e | Local day of week | E | Day of week |
|
|
1773
|
+
* | f | | F* | Day of week in month |
|
|
1774
|
+
* | g* | Modified Julian day | G | Era |
|
|
1775
|
+
* | h | Hour [1-12] | H | Hour [0-23] |
|
|
1776
|
+
* | i! | ISO day of week | I! | ISO week of year |
|
|
1777
|
+
* | j* | Localized hour w/ day period | J* | Localized hour w/o day period |
|
|
1778
|
+
* | k | Hour [1-24] | K | Hour [0-11] |
|
|
1779
|
+
* | l* | (deprecated) | L | Stand-alone month |
|
|
1780
|
+
* | m | Minute | M | Month |
|
|
1781
|
+
* | n | | N | |
|
|
1782
|
+
* | o! | Ordinal number modifier | O | Timezone (GMT) |
|
|
1783
|
+
* | p! | Long localized time | P! | Long localized date |
|
|
1784
|
+
* | q | Stand-alone quarter | Q | Quarter |
|
|
1785
|
+
* | r* | Related Gregorian year | R! | ISO week-numbering year |
|
|
1786
|
+
* | s | Second | S | Fraction of second |
|
|
1787
|
+
* | t! | Seconds timestamp | T! | Milliseconds timestamp |
|
|
1788
|
+
* | u | Extended year | U* | Cyclic year |
|
|
1789
|
+
* | v* | Timezone (generic non-locat.) | V* | Timezone (location) |
|
|
1790
|
+
* | w | Local week of year | W* | Week of month |
|
|
1791
|
+
* | x | Timezone (ISO-8601 w/o Z) | X | Timezone (ISO-8601) |
|
|
1792
|
+
* | y | Year (abs) | Y | Local week-numbering year |
|
|
1793
|
+
* | z | Timezone (specific non-locat.) | Z* | Timezone (aliases) |
|
|
1794
|
+
*
|
|
1795
|
+
* Letters marked by * are not implemented but reserved by Unicode standard.
|
|
1796
|
+
*
|
|
1797
|
+
* Letters marked by ! are non-standard, but implemented by date-fns:
|
|
1798
|
+
* - `o` modifies the previous token to turn it into an ordinal (see `format` docs)
|
|
1799
|
+
* - `i` is ISO day of week. For `i` and `ii` is returns numeric ISO week days,
|
|
1800
|
+
* i.e. 7 for Sunday, 1 for Monday, etc.
|
|
1801
|
+
* - `I` is ISO week of year, as opposed to `w` which is local week of year.
|
|
1802
|
+
* - `R` is ISO week-numbering year, as opposed to `Y` which is local week-numbering year.
|
|
1803
|
+
* `R` is supposed to be used in conjunction with `I` and `i`
|
|
1804
|
+
* for universal ISO week-numbering date, whereas
|
|
1805
|
+
* `Y` is supposed to be used in conjunction with `w` and `e`
|
|
1806
|
+
* for week-numbering date specific to the locale.
|
|
1807
|
+
* - `P` is long localized date format
|
|
1808
|
+
* - `p` is long localized time format
|
|
1809
|
+
*/
|
|
1810
|
+
|
|
1811
|
+
const formatters = {
|
|
1812
|
+
// Era
|
|
1813
|
+
G: function (date, token, localize) {
|
|
1814
|
+
const era = date.getFullYear() > 0 ? 1 : 0;
|
|
1815
|
+
switch (token) {
|
|
1816
|
+
// AD, BC
|
|
1817
|
+
case "G":
|
|
1818
|
+
case "GG":
|
|
1819
|
+
case "GGG":
|
|
1820
|
+
return localize.era(era, { width: "abbreviated" });
|
|
1821
|
+
// A, B
|
|
1822
|
+
case "GGGGG":
|
|
1823
|
+
return localize.era(era, { width: "narrow" });
|
|
1824
|
+
// Anno Domini, Before Christ
|
|
1825
|
+
case "GGGG":
|
|
1826
|
+
default:
|
|
1827
|
+
return localize.era(era, { width: "wide" });
|
|
1828
|
+
}
|
|
1829
|
+
},
|
|
1830
|
+
|
|
1831
|
+
// Year
|
|
1832
|
+
y: function (date, token, localize) {
|
|
1833
|
+
// Ordinal number
|
|
1834
|
+
if (token === "yo") {
|
|
1835
|
+
const signedYear = date.getFullYear();
|
|
1836
|
+
// Returns 1 for 1 BC (which is year 0 in JavaScript)
|
|
1837
|
+
const year = signedYear > 0 ? signedYear : 1 - signedYear;
|
|
1838
|
+
return localize.ordinalNumber(year, { unit: "year" });
|
|
1839
|
+
}
|
|
1840
|
+
|
|
1841
|
+
return lightFormatters.y(date, token);
|
|
1842
|
+
},
|
|
1843
|
+
|
|
1844
|
+
// Local week-numbering year
|
|
1845
|
+
Y: function (date, token, localize, options) {
|
|
1846
|
+
const signedWeekYear = getWeekYear(date, options);
|
|
1847
|
+
// Returns 1 for 1 BC (which is year 0 in JavaScript)
|
|
1848
|
+
const weekYear = signedWeekYear > 0 ? signedWeekYear : 1 - signedWeekYear;
|
|
1849
|
+
|
|
1850
|
+
// Two digit year
|
|
1851
|
+
if (token === "YY") {
|
|
1852
|
+
const twoDigitYear = weekYear % 100;
|
|
1853
|
+
return addLeadingZeros(twoDigitYear, 2);
|
|
1854
|
+
}
|
|
1855
|
+
|
|
1856
|
+
// Ordinal number
|
|
1857
|
+
if (token === "Yo") {
|
|
1858
|
+
return localize.ordinalNumber(weekYear, { unit: "year" });
|
|
1859
|
+
}
|
|
1860
|
+
|
|
1861
|
+
// Padding
|
|
1862
|
+
return addLeadingZeros(weekYear, token.length);
|
|
1863
|
+
},
|
|
1864
|
+
|
|
1865
|
+
// ISO week-numbering year
|
|
1866
|
+
R: function (date, token) {
|
|
1867
|
+
const isoWeekYear = getISOWeekYear(date);
|
|
1868
|
+
|
|
1869
|
+
// Padding
|
|
1870
|
+
return addLeadingZeros(isoWeekYear, token.length);
|
|
1871
|
+
},
|
|
1872
|
+
|
|
1873
|
+
// Extended year. This is a single number designating the year of this calendar system.
|
|
1874
|
+
// The main difference between `y` and `u` localizers are B.C. years:
|
|
1875
|
+
// | Year | `y` | `u` |
|
|
1876
|
+
// |------|-----|-----|
|
|
1877
|
+
// | AC 1 | 1 | 1 |
|
|
1878
|
+
// | BC 1 | 1 | 0 |
|
|
1879
|
+
// | BC 2 | 2 | -1 |
|
|
1880
|
+
// Also `yy` always returns the last two digits of a year,
|
|
1881
|
+
// while `uu` pads single digit years to 2 characters and returns other years unchanged.
|
|
1882
|
+
u: function (date, token) {
|
|
1883
|
+
const year = date.getFullYear();
|
|
1884
|
+
return addLeadingZeros(year, token.length);
|
|
1885
|
+
},
|
|
1886
|
+
|
|
1887
|
+
// Quarter
|
|
1888
|
+
Q: function (date, token, localize) {
|
|
1889
|
+
const quarter = Math.ceil((date.getMonth() + 1) / 3);
|
|
1890
|
+
switch (token) {
|
|
1891
|
+
// 1, 2, 3, 4
|
|
1892
|
+
case "Q":
|
|
1893
|
+
return String(quarter);
|
|
1894
|
+
// 01, 02, 03, 04
|
|
1895
|
+
case "QQ":
|
|
1896
|
+
return addLeadingZeros(quarter, 2);
|
|
1897
|
+
// 1st, 2nd, 3rd, 4th
|
|
1898
|
+
case "Qo":
|
|
1899
|
+
return localize.ordinalNumber(quarter, { unit: "quarter" });
|
|
1900
|
+
// Q1, Q2, Q3, Q4
|
|
1901
|
+
case "QQQ":
|
|
1902
|
+
return localize.quarter(quarter, {
|
|
1903
|
+
width: "abbreviated",
|
|
1904
|
+
context: "formatting",
|
|
1905
|
+
});
|
|
1906
|
+
// 1, 2, 3, 4 (narrow quarter; could be not numerical)
|
|
1907
|
+
case "QQQQQ":
|
|
1908
|
+
return localize.quarter(quarter, {
|
|
1909
|
+
width: "narrow",
|
|
1910
|
+
context: "formatting",
|
|
1911
|
+
});
|
|
1912
|
+
// 1st quarter, 2nd quarter, ...
|
|
1913
|
+
case "QQQQ":
|
|
1914
|
+
default:
|
|
1915
|
+
return localize.quarter(quarter, {
|
|
1916
|
+
width: "wide",
|
|
1917
|
+
context: "formatting",
|
|
1918
|
+
});
|
|
1919
|
+
}
|
|
1920
|
+
},
|
|
1921
|
+
|
|
1922
|
+
// Stand-alone quarter
|
|
1923
|
+
q: function (date, token, localize) {
|
|
1924
|
+
const quarter = Math.ceil((date.getMonth() + 1) / 3);
|
|
1925
|
+
switch (token) {
|
|
1926
|
+
// 1, 2, 3, 4
|
|
1927
|
+
case "q":
|
|
1928
|
+
return String(quarter);
|
|
1929
|
+
// 01, 02, 03, 04
|
|
1930
|
+
case "qq":
|
|
1931
|
+
return addLeadingZeros(quarter, 2);
|
|
1932
|
+
// 1st, 2nd, 3rd, 4th
|
|
1933
|
+
case "qo":
|
|
1934
|
+
return localize.ordinalNumber(quarter, { unit: "quarter" });
|
|
1935
|
+
// Q1, Q2, Q3, Q4
|
|
1936
|
+
case "qqq":
|
|
1937
|
+
return localize.quarter(quarter, {
|
|
1938
|
+
width: "abbreviated",
|
|
1939
|
+
context: "standalone",
|
|
1940
|
+
});
|
|
1941
|
+
// 1, 2, 3, 4 (narrow quarter; could be not numerical)
|
|
1942
|
+
case "qqqqq":
|
|
1943
|
+
return localize.quarter(quarter, {
|
|
1944
|
+
width: "narrow",
|
|
1945
|
+
context: "standalone",
|
|
1946
|
+
});
|
|
1947
|
+
// 1st quarter, 2nd quarter, ...
|
|
1948
|
+
case "qqqq":
|
|
1949
|
+
default:
|
|
1950
|
+
return localize.quarter(quarter, {
|
|
1951
|
+
width: "wide",
|
|
1952
|
+
context: "standalone",
|
|
1953
|
+
});
|
|
1954
|
+
}
|
|
1955
|
+
},
|
|
1956
|
+
|
|
1957
|
+
// Month
|
|
1958
|
+
M: function (date, token, localize) {
|
|
1959
|
+
const month = date.getMonth();
|
|
1960
|
+
switch (token) {
|
|
1961
|
+
case "M":
|
|
1962
|
+
case "MM":
|
|
1963
|
+
return lightFormatters.M(date, token);
|
|
1964
|
+
// 1st, 2nd, ..., 12th
|
|
1965
|
+
case "Mo":
|
|
1966
|
+
return localize.ordinalNumber(month + 1, { unit: "month" });
|
|
1967
|
+
// Jan, Feb, ..., Dec
|
|
1968
|
+
case "MMM":
|
|
1969
|
+
return localize.month(month, {
|
|
1970
|
+
width: "abbreviated",
|
|
1971
|
+
context: "formatting",
|
|
1972
|
+
});
|
|
1973
|
+
// J, F, ..., D
|
|
1974
|
+
case "MMMMM":
|
|
1975
|
+
return localize.month(month, {
|
|
1976
|
+
width: "narrow",
|
|
1977
|
+
context: "formatting",
|
|
1978
|
+
});
|
|
1979
|
+
// January, February, ..., December
|
|
1980
|
+
case "MMMM":
|
|
1981
|
+
default:
|
|
1982
|
+
return localize.month(month, { width: "wide", context: "formatting" });
|
|
1983
|
+
}
|
|
1984
|
+
},
|
|
1985
|
+
|
|
1986
|
+
// Stand-alone month
|
|
1987
|
+
L: function (date, token, localize) {
|
|
1988
|
+
const month = date.getMonth();
|
|
1989
|
+
switch (token) {
|
|
1990
|
+
// 1, 2, ..., 12
|
|
1991
|
+
case "L":
|
|
1992
|
+
return String(month + 1);
|
|
1993
|
+
// 01, 02, ..., 12
|
|
1994
|
+
case "LL":
|
|
1995
|
+
return addLeadingZeros(month + 1, 2);
|
|
1996
|
+
// 1st, 2nd, ..., 12th
|
|
1997
|
+
case "Lo":
|
|
1998
|
+
return localize.ordinalNumber(month + 1, { unit: "month" });
|
|
1999
|
+
// Jan, Feb, ..., Dec
|
|
2000
|
+
case "LLL":
|
|
2001
|
+
return localize.month(month, {
|
|
2002
|
+
width: "abbreviated",
|
|
2003
|
+
context: "standalone",
|
|
2004
|
+
});
|
|
2005
|
+
// J, F, ..., D
|
|
2006
|
+
case "LLLLL":
|
|
2007
|
+
return localize.month(month, {
|
|
2008
|
+
width: "narrow",
|
|
2009
|
+
context: "standalone",
|
|
2010
|
+
});
|
|
2011
|
+
// January, February, ..., December
|
|
2012
|
+
case "LLLL":
|
|
2013
|
+
default:
|
|
2014
|
+
return localize.month(month, { width: "wide", context: "standalone" });
|
|
2015
|
+
}
|
|
2016
|
+
},
|
|
2017
|
+
|
|
2018
|
+
// Local week of year
|
|
2019
|
+
w: function (date, token, localize, options) {
|
|
2020
|
+
const week = getWeek(date, options);
|
|
2021
|
+
|
|
2022
|
+
if (token === "wo") {
|
|
2023
|
+
return localize.ordinalNumber(week, { unit: "week" });
|
|
2024
|
+
}
|
|
2025
|
+
|
|
2026
|
+
return addLeadingZeros(week, token.length);
|
|
2027
|
+
},
|
|
2028
|
+
|
|
2029
|
+
// ISO week of year
|
|
2030
|
+
I: function (date, token, localize) {
|
|
2031
|
+
const isoWeek = getISOWeek(date);
|
|
2032
|
+
|
|
2033
|
+
if (token === "Io") {
|
|
2034
|
+
return localize.ordinalNumber(isoWeek, { unit: "week" });
|
|
2035
|
+
}
|
|
2036
|
+
|
|
2037
|
+
return addLeadingZeros(isoWeek, token.length);
|
|
2038
|
+
},
|
|
2039
|
+
|
|
2040
|
+
// Day of the month
|
|
2041
|
+
d: function (date, token, localize) {
|
|
2042
|
+
if (token === "do") {
|
|
2043
|
+
return localize.ordinalNumber(date.getDate(), { unit: "date" });
|
|
2044
|
+
}
|
|
2045
|
+
|
|
2046
|
+
return lightFormatters.d(date, token);
|
|
2047
|
+
},
|
|
2048
|
+
|
|
2049
|
+
// Day of year
|
|
2050
|
+
D: function (date, token, localize) {
|
|
2051
|
+
const dayOfYear = getDayOfYear(date);
|
|
2052
|
+
|
|
2053
|
+
if (token === "Do") {
|
|
2054
|
+
return localize.ordinalNumber(dayOfYear, { unit: "dayOfYear" });
|
|
2055
|
+
}
|
|
2056
|
+
|
|
2057
|
+
return addLeadingZeros(dayOfYear, token.length);
|
|
2058
|
+
},
|
|
2059
|
+
|
|
2060
|
+
// Day of week
|
|
2061
|
+
E: function (date, token, localize) {
|
|
2062
|
+
const dayOfWeek = date.getDay();
|
|
2063
|
+
switch (token) {
|
|
2064
|
+
// Tue
|
|
2065
|
+
case "E":
|
|
2066
|
+
case "EE":
|
|
2067
|
+
case "EEE":
|
|
2068
|
+
return localize.day(dayOfWeek, {
|
|
2069
|
+
width: "abbreviated",
|
|
2070
|
+
context: "formatting",
|
|
2071
|
+
});
|
|
2072
|
+
// T
|
|
2073
|
+
case "EEEEE":
|
|
2074
|
+
return localize.day(dayOfWeek, {
|
|
2075
|
+
width: "narrow",
|
|
2076
|
+
context: "formatting",
|
|
2077
|
+
});
|
|
2078
|
+
// Tu
|
|
2079
|
+
case "EEEEEE":
|
|
2080
|
+
return localize.day(dayOfWeek, {
|
|
2081
|
+
width: "short",
|
|
2082
|
+
context: "formatting",
|
|
2083
|
+
});
|
|
2084
|
+
// Tuesday
|
|
2085
|
+
case "EEEE":
|
|
2086
|
+
default:
|
|
2087
|
+
return localize.day(dayOfWeek, {
|
|
2088
|
+
width: "wide",
|
|
2089
|
+
context: "formatting",
|
|
2090
|
+
});
|
|
2091
|
+
}
|
|
2092
|
+
},
|
|
2093
|
+
|
|
2094
|
+
// Local day of week
|
|
2095
|
+
e: function (date, token, localize, options) {
|
|
2096
|
+
const dayOfWeek = date.getDay();
|
|
2097
|
+
const localDayOfWeek = (dayOfWeek - options.weekStartsOn + 8) % 7 || 7;
|
|
2098
|
+
switch (token) {
|
|
2099
|
+
// Numerical value (Nth day of week with current locale or weekStartsOn)
|
|
2100
|
+
case "e":
|
|
2101
|
+
return String(localDayOfWeek);
|
|
2102
|
+
// Padded numerical value
|
|
2103
|
+
case "ee":
|
|
2104
|
+
return addLeadingZeros(localDayOfWeek, 2);
|
|
2105
|
+
// 1st, 2nd, ..., 7th
|
|
2106
|
+
case "eo":
|
|
2107
|
+
return localize.ordinalNumber(localDayOfWeek, { unit: "day" });
|
|
2108
|
+
case "eee":
|
|
2109
|
+
return localize.day(dayOfWeek, {
|
|
2110
|
+
width: "abbreviated",
|
|
2111
|
+
context: "formatting",
|
|
2112
|
+
});
|
|
2113
|
+
// T
|
|
2114
|
+
case "eeeee":
|
|
2115
|
+
return localize.day(dayOfWeek, {
|
|
2116
|
+
width: "narrow",
|
|
2117
|
+
context: "formatting",
|
|
2118
|
+
});
|
|
2119
|
+
// Tu
|
|
2120
|
+
case "eeeeee":
|
|
2121
|
+
return localize.day(dayOfWeek, {
|
|
2122
|
+
width: "short",
|
|
2123
|
+
context: "formatting",
|
|
2124
|
+
});
|
|
2125
|
+
// Tuesday
|
|
2126
|
+
case "eeee":
|
|
2127
|
+
default:
|
|
2128
|
+
return localize.day(dayOfWeek, {
|
|
2129
|
+
width: "wide",
|
|
2130
|
+
context: "formatting",
|
|
2131
|
+
});
|
|
2132
|
+
}
|
|
2133
|
+
},
|
|
2134
|
+
|
|
2135
|
+
// Stand-alone local day of week
|
|
2136
|
+
c: function (date, token, localize, options) {
|
|
2137
|
+
const dayOfWeek = date.getDay();
|
|
2138
|
+
const localDayOfWeek = (dayOfWeek - options.weekStartsOn + 8) % 7 || 7;
|
|
2139
|
+
switch (token) {
|
|
2140
|
+
// Numerical value (same as in `e`)
|
|
2141
|
+
case "c":
|
|
2142
|
+
return String(localDayOfWeek);
|
|
2143
|
+
// Padded numerical value
|
|
2144
|
+
case "cc":
|
|
2145
|
+
return addLeadingZeros(localDayOfWeek, token.length);
|
|
2146
|
+
// 1st, 2nd, ..., 7th
|
|
2147
|
+
case "co":
|
|
2148
|
+
return localize.ordinalNumber(localDayOfWeek, { unit: "day" });
|
|
2149
|
+
case "ccc":
|
|
2150
|
+
return localize.day(dayOfWeek, {
|
|
2151
|
+
width: "abbreviated",
|
|
2152
|
+
context: "standalone",
|
|
2153
|
+
});
|
|
2154
|
+
// T
|
|
2155
|
+
case "ccccc":
|
|
2156
|
+
return localize.day(dayOfWeek, {
|
|
2157
|
+
width: "narrow",
|
|
2158
|
+
context: "standalone",
|
|
2159
|
+
});
|
|
2160
|
+
// Tu
|
|
2161
|
+
case "cccccc":
|
|
2162
|
+
return localize.day(dayOfWeek, {
|
|
2163
|
+
width: "short",
|
|
2164
|
+
context: "standalone",
|
|
2165
|
+
});
|
|
2166
|
+
// Tuesday
|
|
2167
|
+
case "cccc":
|
|
2168
|
+
default:
|
|
2169
|
+
return localize.day(dayOfWeek, {
|
|
2170
|
+
width: "wide",
|
|
2171
|
+
context: "standalone",
|
|
2172
|
+
});
|
|
2173
|
+
}
|
|
2174
|
+
},
|
|
2175
|
+
|
|
2176
|
+
// ISO day of week
|
|
2177
|
+
i: function (date, token, localize) {
|
|
2178
|
+
const dayOfWeek = date.getDay();
|
|
2179
|
+
const isoDayOfWeek = dayOfWeek === 0 ? 7 : dayOfWeek;
|
|
2180
|
+
switch (token) {
|
|
2181
|
+
// 2
|
|
2182
|
+
case "i":
|
|
2183
|
+
return String(isoDayOfWeek);
|
|
2184
|
+
// 02
|
|
2185
|
+
case "ii":
|
|
2186
|
+
return addLeadingZeros(isoDayOfWeek, token.length);
|
|
2187
|
+
// 2nd
|
|
2188
|
+
case "io":
|
|
2189
|
+
return localize.ordinalNumber(isoDayOfWeek, { unit: "day" });
|
|
2190
|
+
// Tue
|
|
2191
|
+
case "iii":
|
|
2192
|
+
return localize.day(dayOfWeek, {
|
|
2193
|
+
width: "abbreviated",
|
|
2194
|
+
context: "formatting",
|
|
2195
|
+
});
|
|
2196
|
+
// T
|
|
2197
|
+
case "iiiii":
|
|
2198
|
+
return localize.day(dayOfWeek, {
|
|
2199
|
+
width: "narrow",
|
|
2200
|
+
context: "formatting",
|
|
2201
|
+
});
|
|
2202
|
+
// Tu
|
|
2203
|
+
case "iiiiii":
|
|
2204
|
+
return localize.day(dayOfWeek, {
|
|
2205
|
+
width: "short",
|
|
2206
|
+
context: "formatting",
|
|
2207
|
+
});
|
|
2208
|
+
// Tuesday
|
|
2209
|
+
case "iiii":
|
|
2210
|
+
default:
|
|
2211
|
+
return localize.day(dayOfWeek, {
|
|
2212
|
+
width: "wide",
|
|
2213
|
+
context: "formatting",
|
|
2214
|
+
});
|
|
2215
|
+
}
|
|
2216
|
+
},
|
|
2217
|
+
|
|
2218
|
+
// AM or PM
|
|
2219
|
+
a: function (date, token, localize) {
|
|
2220
|
+
const hours = date.getHours();
|
|
2221
|
+
const dayPeriodEnumValue = hours / 12 >= 1 ? "pm" : "am";
|
|
2222
|
+
|
|
2223
|
+
switch (token) {
|
|
2224
|
+
case "a":
|
|
2225
|
+
case "aa":
|
|
2226
|
+
return localize.dayPeriod(dayPeriodEnumValue, {
|
|
2227
|
+
width: "abbreviated",
|
|
2228
|
+
context: "formatting",
|
|
2229
|
+
});
|
|
2230
|
+
case "aaa":
|
|
2231
|
+
return localize
|
|
2232
|
+
.dayPeriod(dayPeriodEnumValue, {
|
|
2233
|
+
width: "abbreviated",
|
|
2234
|
+
context: "formatting",
|
|
2235
|
+
})
|
|
2236
|
+
.toLowerCase();
|
|
2237
|
+
case "aaaaa":
|
|
2238
|
+
return localize.dayPeriod(dayPeriodEnumValue, {
|
|
2239
|
+
width: "narrow",
|
|
2240
|
+
context: "formatting",
|
|
2241
|
+
});
|
|
2242
|
+
case "aaaa":
|
|
2243
|
+
default:
|
|
2244
|
+
return localize.dayPeriod(dayPeriodEnumValue, {
|
|
2245
|
+
width: "wide",
|
|
2246
|
+
context: "formatting",
|
|
2247
|
+
});
|
|
2248
|
+
}
|
|
2249
|
+
},
|
|
2250
|
+
|
|
2251
|
+
// AM, PM, midnight, noon
|
|
2252
|
+
b: function (date, token, localize) {
|
|
2253
|
+
const hours = date.getHours();
|
|
2254
|
+
let dayPeriodEnumValue;
|
|
2255
|
+
if (hours === 12) {
|
|
2256
|
+
dayPeriodEnumValue = dayPeriodEnum.noon;
|
|
2257
|
+
} else if (hours === 0) {
|
|
2258
|
+
dayPeriodEnumValue = dayPeriodEnum.midnight;
|
|
2259
|
+
} else {
|
|
2260
|
+
dayPeriodEnumValue = hours / 12 >= 1 ? "pm" : "am";
|
|
2261
|
+
}
|
|
2262
|
+
|
|
2263
|
+
switch (token) {
|
|
2264
|
+
case "b":
|
|
2265
|
+
case "bb":
|
|
2266
|
+
return localize.dayPeriod(dayPeriodEnumValue, {
|
|
2267
|
+
width: "abbreviated",
|
|
2268
|
+
context: "formatting",
|
|
2269
|
+
});
|
|
2270
|
+
case "bbb":
|
|
2271
|
+
return localize
|
|
2272
|
+
.dayPeriod(dayPeriodEnumValue, {
|
|
2273
|
+
width: "abbreviated",
|
|
2274
|
+
context: "formatting",
|
|
2275
|
+
})
|
|
2276
|
+
.toLowerCase();
|
|
2277
|
+
case "bbbbb":
|
|
2278
|
+
return localize.dayPeriod(dayPeriodEnumValue, {
|
|
2279
|
+
width: "narrow",
|
|
2280
|
+
context: "formatting",
|
|
2281
|
+
});
|
|
2282
|
+
case "bbbb":
|
|
2283
|
+
default:
|
|
2284
|
+
return localize.dayPeriod(dayPeriodEnumValue, {
|
|
2285
|
+
width: "wide",
|
|
2286
|
+
context: "formatting",
|
|
2287
|
+
});
|
|
2288
|
+
}
|
|
2289
|
+
},
|
|
2290
|
+
|
|
2291
|
+
// in the morning, in the afternoon, in the evening, at night
|
|
2292
|
+
B: function (date, token, localize) {
|
|
2293
|
+
const hours = date.getHours();
|
|
2294
|
+
let dayPeriodEnumValue;
|
|
2295
|
+
if (hours >= 17) {
|
|
2296
|
+
dayPeriodEnumValue = dayPeriodEnum.evening;
|
|
2297
|
+
} else if (hours >= 12) {
|
|
2298
|
+
dayPeriodEnumValue = dayPeriodEnum.afternoon;
|
|
2299
|
+
} else if (hours >= 4) {
|
|
2300
|
+
dayPeriodEnumValue = dayPeriodEnum.morning;
|
|
2301
|
+
} else {
|
|
2302
|
+
dayPeriodEnumValue = dayPeriodEnum.night;
|
|
2303
|
+
}
|
|
2304
|
+
|
|
2305
|
+
switch (token) {
|
|
2306
|
+
case "B":
|
|
2307
|
+
case "BB":
|
|
2308
|
+
case "BBB":
|
|
2309
|
+
return localize.dayPeriod(dayPeriodEnumValue, {
|
|
2310
|
+
width: "abbreviated",
|
|
2311
|
+
context: "formatting",
|
|
2312
|
+
});
|
|
2313
|
+
case "BBBBB":
|
|
2314
|
+
return localize.dayPeriod(dayPeriodEnumValue, {
|
|
2315
|
+
width: "narrow",
|
|
2316
|
+
context: "formatting",
|
|
2317
|
+
});
|
|
2318
|
+
case "BBBB":
|
|
2319
|
+
default:
|
|
2320
|
+
return localize.dayPeriod(dayPeriodEnumValue, {
|
|
2321
|
+
width: "wide",
|
|
2322
|
+
context: "formatting",
|
|
2323
|
+
});
|
|
2324
|
+
}
|
|
2325
|
+
},
|
|
2326
|
+
|
|
2327
|
+
// Hour [1-12]
|
|
2328
|
+
h: function (date, token, localize) {
|
|
2329
|
+
if (token === "ho") {
|
|
2330
|
+
let hours = date.getHours() % 12;
|
|
2331
|
+
if (hours === 0) hours = 12;
|
|
2332
|
+
return localize.ordinalNumber(hours, { unit: "hour" });
|
|
2333
|
+
}
|
|
2334
|
+
|
|
2335
|
+
return lightFormatters.h(date, token);
|
|
2336
|
+
},
|
|
2337
|
+
|
|
2338
|
+
// Hour [0-23]
|
|
2339
|
+
H: function (date, token, localize) {
|
|
2340
|
+
if (token === "Ho") {
|
|
2341
|
+
return localize.ordinalNumber(date.getHours(), { unit: "hour" });
|
|
2342
|
+
}
|
|
2343
|
+
|
|
2344
|
+
return lightFormatters.H(date, token);
|
|
2345
|
+
},
|
|
2346
|
+
|
|
2347
|
+
// Hour [0-11]
|
|
2348
|
+
K: function (date, token, localize) {
|
|
2349
|
+
const hours = date.getHours() % 12;
|
|
2350
|
+
|
|
2351
|
+
if (token === "Ko") {
|
|
2352
|
+
return localize.ordinalNumber(hours, { unit: "hour" });
|
|
2353
|
+
}
|
|
2354
|
+
|
|
2355
|
+
return addLeadingZeros(hours, token.length);
|
|
2356
|
+
},
|
|
2357
|
+
|
|
2358
|
+
// Hour [1-24]
|
|
2359
|
+
k: function (date, token, localize) {
|
|
2360
|
+
let hours = date.getHours();
|
|
2361
|
+
if (hours === 0) hours = 24;
|
|
2362
|
+
|
|
2363
|
+
if (token === "ko") {
|
|
2364
|
+
return localize.ordinalNumber(hours, { unit: "hour" });
|
|
2365
|
+
}
|
|
2366
|
+
|
|
2367
|
+
return addLeadingZeros(hours, token.length);
|
|
2368
|
+
},
|
|
2369
|
+
|
|
2370
|
+
// Minute
|
|
2371
|
+
m: function (date, token, localize) {
|
|
2372
|
+
if (token === "mo") {
|
|
2373
|
+
return localize.ordinalNumber(date.getMinutes(), { unit: "minute" });
|
|
2374
|
+
}
|
|
2375
|
+
|
|
2376
|
+
return lightFormatters.m(date, token);
|
|
2377
|
+
},
|
|
2378
|
+
|
|
2379
|
+
// Second
|
|
2380
|
+
s: function (date, token, localize) {
|
|
2381
|
+
if (token === "so") {
|
|
2382
|
+
return localize.ordinalNumber(date.getSeconds(), { unit: "second" });
|
|
2383
|
+
}
|
|
2384
|
+
|
|
2385
|
+
return lightFormatters.s(date, token);
|
|
2386
|
+
},
|
|
2387
|
+
|
|
2388
|
+
// Fraction of second
|
|
2389
|
+
S: function (date, token) {
|
|
2390
|
+
return lightFormatters.S(date, token);
|
|
2391
|
+
},
|
|
2392
|
+
|
|
2393
|
+
// Timezone (ISO-8601. If offset is 0, output is always `'Z'`)
|
|
2394
|
+
X: function (date, token, _localize) {
|
|
2395
|
+
const timezoneOffset = date.getTimezoneOffset();
|
|
2396
|
+
|
|
2397
|
+
if (timezoneOffset === 0) {
|
|
2398
|
+
return "Z";
|
|
2399
|
+
}
|
|
2400
|
+
|
|
2401
|
+
switch (token) {
|
|
2402
|
+
// Hours and optional minutes
|
|
2403
|
+
case "X":
|
|
2404
|
+
return formatTimezoneWithOptionalMinutes(timezoneOffset);
|
|
2405
|
+
|
|
2406
|
+
// Hours, minutes and optional seconds without `:` delimiter
|
|
2407
|
+
// Note: neither ISO-8601 nor JavaScript supports seconds in timezone offsets
|
|
2408
|
+
// so this token always has the same output as `XX`
|
|
2409
|
+
case "XXXX":
|
|
2410
|
+
case "XX": // Hours and minutes without `:` delimiter
|
|
2411
|
+
return formatTimezone(timezoneOffset);
|
|
2412
|
+
|
|
2413
|
+
// Hours, minutes and optional seconds with `:` delimiter
|
|
2414
|
+
// Note: neither ISO-8601 nor JavaScript supports seconds in timezone offsets
|
|
2415
|
+
// so this token always has the same output as `XXX`
|
|
2416
|
+
case "XXXXX":
|
|
2417
|
+
case "XXX": // Hours and minutes with `:` delimiter
|
|
2418
|
+
default:
|
|
2419
|
+
return formatTimezone(timezoneOffset, ":");
|
|
2420
|
+
}
|
|
2421
|
+
},
|
|
2422
|
+
|
|
2423
|
+
// Timezone (ISO-8601. If offset is 0, output is `'+00:00'` or equivalent)
|
|
2424
|
+
x: function (date, token, _localize) {
|
|
2425
|
+
const timezoneOffset = date.getTimezoneOffset();
|
|
2426
|
+
|
|
2427
|
+
switch (token) {
|
|
2428
|
+
// Hours and optional minutes
|
|
2429
|
+
case "x":
|
|
2430
|
+
return formatTimezoneWithOptionalMinutes(timezoneOffset);
|
|
2431
|
+
|
|
2432
|
+
// Hours, minutes and optional seconds without `:` delimiter
|
|
2433
|
+
// Note: neither ISO-8601 nor JavaScript supports seconds in timezone offsets
|
|
2434
|
+
// so this token always has the same output as `xx`
|
|
2435
|
+
case "xxxx":
|
|
2436
|
+
case "xx": // Hours and minutes without `:` delimiter
|
|
2437
|
+
return formatTimezone(timezoneOffset);
|
|
2438
|
+
|
|
2439
|
+
// Hours, minutes and optional seconds with `:` delimiter
|
|
2440
|
+
// Note: neither ISO-8601 nor JavaScript supports seconds in timezone offsets
|
|
2441
|
+
// so this token always has the same output as `xxx`
|
|
2442
|
+
case "xxxxx":
|
|
2443
|
+
case "xxx": // Hours and minutes with `:` delimiter
|
|
2444
|
+
default:
|
|
2445
|
+
return formatTimezone(timezoneOffset, ":");
|
|
2446
|
+
}
|
|
2447
|
+
},
|
|
2448
|
+
|
|
2449
|
+
// Timezone (GMT)
|
|
2450
|
+
O: function (date, token, _localize) {
|
|
2451
|
+
const timezoneOffset = date.getTimezoneOffset();
|
|
2452
|
+
|
|
2453
|
+
switch (token) {
|
|
2454
|
+
// Short
|
|
2455
|
+
case "O":
|
|
2456
|
+
case "OO":
|
|
2457
|
+
case "OOO":
|
|
2458
|
+
return "GMT" + formatTimezoneShort(timezoneOffset, ":");
|
|
2459
|
+
// Long
|
|
2460
|
+
case "OOOO":
|
|
2461
|
+
default:
|
|
2462
|
+
return "GMT" + formatTimezone(timezoneOffset, ":");
|
|
2463
|
+
}
|
|
2464
|
+
},
|
|
2465
|
+
|
|
2466
|
+
// Timezone (specific non-location)
|
|
2467
|
+
z: function (date, token, _localize) {
|
|
2468
|
+
const timezoneOffset = date.getTimezoneOffset();
|
|
2469
|
+
|
|
2470
|
+
switch (token) {
|
|
2471
|
+
// Short
|
|
2472
|
+
case "z":
|
|
2473
|
+
case "zz":
|
|
2474
|
+
case "zzz":
|
|
2475
|
+
return "GMT" + formatTimezoneShort(timezoneOffset, ":");
|
|
2476
|
+
// Long
|
|
2477
|
+
case "zzzz":
|
|
2478
|
+
default:
|
|
2479
|
+
return "GMT" + formatTimezone(timezoneOffset, ":");
|
|
2480
|
+
}
|
|
2481
|
+
},
|
|
2482
|
+
|
|
2483
|
+
// Seconds timestamp
|
|
2484
|
+
t: function (date, token, _localize) {
|
|
2485
|
+
const timestamp = Math.trunc(date.getTime() / 1000);
|
|
2486
|
+
return addLeadingZeros(timestamp, token.length);
|
|
2487
|
+
},
|
|
2488
|
+
|
|
2489
|
+
// Milliseconds timestamp
|
|
2490
|
+
T: function (date, token, _localize) {
|
|
2491
|
+
const timestamp = date.getTime();
|
|
2492
|
+
return addLeadingZeros(timestamp, token.length);
|
|
2493
|
+
},
|
|
2494
|
+
};
|
|
2495
|
+
|
|
2496
|
+
function formatTimezoneShort(offset, delimiter = "") {
|
|
2497
|
+
const sign = offset > 0 ? "-" : "+";
|
|
2498
|
+
const absOffset = Math.abs(offset);
|
|
2499
|
+
const hours = Math.trunc(absOffset / 60);
|
|
2500
|
+
const minutes = absOffset % 60;
|
|
2501
|
+
if (minutes === 0) {
|
|
2502
|
+
return sign + String(hours);
|
|
2503
|
+
}
|
|
2504
|
+
return sign + String(hours) + delimiter + addLeadingZeros(minutes, 2);
|
|
2505
|
+
}
|
|
2506
|
+
|
|
2507
|
+
function formatTimezoneWithOptionalMinutes(offset, delimiter) {
|
|
2508
|
+
if (offset % 60 === 0) {
|
|
2509
|
+
const sign = offset > 0 ? "-" : "+";
|
|
2510
|
+
return sign + addLeadingZeros(Math.abs(offset) / 60, 2);
|
|
2511
|
+
}
|
|
2512
|
+
return formatTimezone(offset, delimiter);
|
|
2513
|
+
}
|
|
2514
|
+
|
|
2515
|
+
function formatTimezone(offset, delimiter = "") {
|
|
2516
|
+
const sign = offset > 0 ? "-" : "+";
|
|
2517
|
+
const absOffset = Math.abs(offset);
|
|
2518
|
+
const hours = addLeadingZeros(Math.trunc(absOffset / 60), 2);
|
|
2519
|
+
const minutes = addLeadingZeros(absOffset % 60, 2);
|
|
2520
|
+
return sign + hours + delimiter + minutes;
|
|
2521
|
+
}
|
|
2522
|
+
|
|
2523
|
+
const dateLongFormatter = (pattern, formatLong) => {
|
|
2524
|
+
switch (pattern) {
|
|
2525
|
+
case "P":
|
|
2526
|
+
return formatLong.date({ width: "short" });
|
|
2527
|
+
case "PP":
|
|
2528
|
+
return formatLong.date({ width: "medium" });
|
|
2529
|
+
case "PPP":
|
|
2530
|
+
return formatLong.date({ width: "long" });
|
|
2531
|
+
case "PPPP":
|
|
2532
|
+
default:
|
|
2533
|
+
return formatLong.date({ width: "full" });
|
|
2534
|
+
}
|
|
2535
|
+
};
|
|
2536
|
+
|
|
2537
|
+
const timeLongFormatter = (pattern, formatLong) => {
|
|
2538
|
+
switch (pattern) {
|
|
2539
|
+
case "p":
|
|
2540
|
+
return formatLong.time({ width: "short" });
|
|
2541
|
+
case "pp":
|
|
2542
|
+
return formatLong.time({ width: "medium" });
|
|
2543
|
+
case "ppp":
|
|
2544
|
+
return formatLong.time({ width: "long" });
|
|
2545
|
+
case "pppp":
|
|
2546
|
+
default:
|
|
2547
|
+
return formatLong.time({ width: "full" });
|
|
2548
|
+
}
|
|
2549
|
+
};
|
|
2550
|
+
|
|
2551
|
+
const dateTimeLongFormatter = (pattern, formatLong) => {
|
|
2552
|
+
const matchResult = pattern.match(/(P+)(p+)?/) || [];
|
|
2553
|
+
const datePattern = matchResult[1];
|
|
2554
|
+
const timePattern = matchResult[2];
|
|
2555
|
+
|
|
2556
|
+
if (!timePattern) {
|
|
2557
|
+
return dateLongFormatter(pattern, formatLong);
|
|
2558
|
+
}
|
|
2559
|
+
|
|
2560
|
+
let dateTimeFormat;
|
|
2561
|
+
|
|
2562
|
+
switch (datePattern) {
|
|
2563
|
+
case "P":
|
|
2564
|
+
dateTimeFormat = formatLong.dateTime({ width: "short" });
|
|
2565
|
+
break;
|
|
2566
|
+
case "PP":
|
|
2567
|
+
dateTimeFormat = formatLong.dateTime({ width: "medium" });
|
|
2568
|
+
break;
|
|
2569
|
+
case "PPP":
|
|
2570
|
+
dateTimeFormat = formatLong.dateTime({ width: "long" });
|
|
2571
|
+
break;
|
|
2572
|
+
case "PPPP":
|
|
2573
|
+
default:
|
|
2574
|
+
dateTimeFormat = formatLong.dateTime({ width: "full" });
|
|
2575
|
+
break;
|
|
2576
|
+
}
|
|
2577
|
+
|
|
2578
|
+
return dateTimeFormat
|
|
2579
|
+
.replace("{{date}}", dateLongFormatter(datePattern, formatLong))
|
|
2580
|
+
.replace("{{time}}", timeLongFormatter(timePattern, formatLong));
|
|
2581
|
+
};
|
|
2582
|
+
|
|
2583
|
+
const longFormatters = {
|
|
2584
|
+
p: timeLongFormatter,
|
|
2585
|
+
P: dateTimeLongFormatter,
|
|
2586
|
+
};
|
|
2587
|
+
|
|
2588
|
+
const dayOfYearTokenRE = /^D+$/;
|
|
2589
|
+
const weekYearTokenRE = /^Y+$/;
|
|
2590
|
+
|
|
2591
|
+
const throwTokens = ["D", "DD", "YY", "YYYY"];
|
|
2592
|
+
|
|
2593
|
+
function isProtectedDayOfYearToken(token) {
|
|
2594
|
+
return dayOfYearTokenRE.test(token);
|
|
2595
|
+
}
|
|
2596
|
+
|
|
2597
|
+
function isProtectedWeekYearToken(token) {
|
|
2598
|
+
return weekYearTokenRE.test(token);
|
|
2599
|
+
}
|
|
2600
|
+
|
|
2601
|
+
function warnOrThrowProtectedError(token, format, input) {
|
|
2602
|
+
const _message = message(token, format, input);
|
|
2603
|
+
console.warn(_message);
|
|
2604
|
+
if (throwTokens.includes(token)) throw new RangeError(_message);
|
|
2605
|
+
}
|
|
2606
|
+
|
|
2607
|
+
function message(token, format, input) {
|
|
2608
|
+
const subject = token[0] === "Y" ? "years" : "days of the month";
|
|
2609
|
+
return `Use \`${token.toLowerCase()}\` instead of \`${token}\` (in \`${format}\`) for formatting ${subject} to the input \`${input}\`; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md`;
|
|
2610
|
+
}
|
|
2611
|
+
|
|
2612
|
+
// This RegExp consists of three parts separated by `|`:
|
|
2613
|
+
// - [yYQqMLwIdDecihHKkms]o matches any available ordinal number token
|
|
2614
|
+
// (one of the certain letters followed by `o`)
|
|
2615
|
+
// - (\w)\1* matches any sequences of the same letter
|
|
2616
|
+
// - '' matches two quote characters in a row
|
|
2617
|
+
// - '(''|[^'])+('|$) matches anything surrounded by two quote characters ('),
|
|
2618
|
+
// except a single quote symbol, which ends the sequence.
|
|
2619
|
+
// Two quote characters do not end the sequence.
|
|
2620
|
+
// If there is no matching single quote
|
|
2621
|
+
// then the sequence will continue until the end of the string.
|
|
2622
|
+
// - . matches any single character unmatched by previous parts of the RegExps
|
|
2623
|
+
const formattingTokensRegExp =
|
|
2624
|
+
/[yYQqMLwIdDecihHKkms]o|(\w)\1*|''|'(''|[^'])+('|$)|./g;
|
|
2625
|
+
|
|
2626
|
+
// This RegExp catches symbols escaped by quotes, and also
|
|
2627
|
+
// sequences of symbols P, p, and the combinations like `PPPPPPPppppp`
|
|
2628
|
+
const longFormattingTokensRegExp = /P+p+|P+|p+|''|'(''|[^'])+('|$)|./g;
|
|
2629
|
+
|
|
2630
|
+
const escapedStringRegExp = /^'([^]*?)'?$/;
|
|
2631
|
+
const doubleQuoteRegExp = /''/g;
|
|
2632
|
+
const unescapedLatinCharacterRegExp = /[a-zA-Z]/;
|
|
2633
|
+
|
|
2634
|
+
/**
|
|
2635
|
+
* The {@link format} function options.
|
|
2636
|
+
*/
|
|
2637
|
+
|
|
2638
|
+
/**
|
|
2639
|
+
* @name format
|
|
2640
|
+
* @alias formatDate
|
|
2641
|
+
* @category Common Helpers
|
|
2642
|
+
* @summary Format the date.
|
|
2643
|
+
*
|
|
2644
|
+
* @description
|
|
2645
|
+
* Return the formatted date string in the given format. The result may vary by locale.
|
|
2646
|
+
*
|
|
2647
|
+
* > ⚠️ Please note that the `format` tokens differ from Moment.js and other libraries.
|
|
2648
|
+
* > See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
|
|
2649
|
+
*
|
|
2650
|
+
* The characters wrapped between two single quotes characters (') are escaped.
|
|
2651
|
+
* Two single quotes in a row, whether inside or outside a quoted sequence, represent a 'real' single quote.
|
|
2652
|
+
* (see the last example)
|
|
2653
|
+
*
|
|
2654
|
+
* Format of the string is based on Unicode Technical Standard #35:
|
|
2655
|
+
* https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table
|
|
2656
|
+
* with a few additions (see note 7 below the table).
|
|
2657
|
+
*
|
|
2658
|
+
* Accepted patterns:
|
|
2659
|
+
* | Unit | Pattern | Result examples | Notes |
|
|
2660
|
+
* |---------------------------------|---------|-----------------------------------|-------|
|
|
2661
|
+
* | Era | G..GGG | AD, BC | |
|
|
2662
|
+
* | | GGGG | Anno Domini, Before Christ | 2 |
|
|
2663
|
+
* | | GGGGG | A, B | |
|
|
2664
|
+
* | Calendar year | y | 44, 1, 1900, 2017 | 5 |
|
|
2665
|
+
* | | yo | 44th, 1st, 0th, 17th | 5,7 |
|
|
2666
|
+
* | | yy | 44, 01, 00, 17 | 5 |
|
|
2667
|
+
* | | yyy | 044, 001, 1900, 2017 | 5 |
|
|
2668
|
+
* | | yyyy | 0044, 0001, 1900, 2017 | 5 |
|
|
2669
|
+
* | | yyyyy | ... | 3,5 |
|
|
2670
|
+
* | Local week-numbering year | Y | 44, 1, 1900, 2017 | 5 |
|
|
2671
|
+
* | | Yo | 44th, 1st, 1900th, 2017th | 5,7 |
|
|
2672
|
+
* | | YY | 44, 01, 00, 17 | 5,8 |
|
|
2673
|
+
* | | YYY | 044, 001, 1900, 2017 | 5 |
|
|
2674
|
+
* | | YYYY | 0044, 0001, 1900, 2017 | 5,8 |
|
|
2675
|
+
* | | YYYYY | ... | 3,5 |
|
|
2676
|
+
* | ISO week-numbering year | R | -43, 0, 1, 1900, 2017 | 5,7 |
|
|
2677
|
+
* | | RR | -43, 00, 01, 1900, 2017 | 5,7 |
|
|
2678
|
+
* | | RRR | -043, 000, 001, 1900, 2017 | 5,7 |
|
|
2679
|
+
* | | RRRR | -0043, 0000, 0001, 1900, 2017 | 5,7 |
|
|
2680
|
+
* | | RRRRR | ... | 3,5,7 |
|
|
2681
|
+
* | Extended year | u | -43, 0, 1, 1900, 2017 | 5 |
|
|
2682
|
+
* | | uu | -43, 01, 1900, 2017 | 5 |
|
|
2683
|
+
* | | uuu | -043, 001, 1900, 2017 | 5 |
|
|
2684
|
+
* | | uuuu | -0043, 0001, 1900, 2017 | 5 |
|
|
2685
|
+
* | | uuuuu | ... | 3,5 |
|
|
2686
|
+
* | Quarter (formatting) | Q | 1, 2, 3, 4 | |
|
|
2687
|
+
* | | Qo | 1st, 2nd, 3rd, 4th | 7 |
|
|
2688
|
+
* | | QQ | 01, 02, 03, 04 | |
|
|
2689
|
+
* | | QQQ | Q1, Q2, Q3, Q4 | |
|
|
2690
|
+
* | | QQQQ | 1st quarter, 2nd quarter, ... | 2 |
|
|
2691
|
+
* | | QQQQQ | 1, 2, 3, 4 | 4 |
|
|
2692
|
+
* | Quarter (stand-alone) | q | 1, 2, 3, 4 | |
|
|
2693
|
+
* | | qo | 1st, 2nd, 3rd, 4th | 7 |
|
|
2694
|
+
* | | qq | 01, 02, 03, 04 | |
|
|
2695
|
+
* | | qqq | Q1, Q2, Q3, Q4 | |
|
|
2696
|
+
* | | qqqq | 1st quarter, 2nd quarter, ... | 2 |
|
|
2697
|
+
* | | qqqqq | 1, 2, 3, 4 | 4 |
|
|
2698
|
+
* | Month (formatting) | M | 1, 2, ..., 12 | |
|
|
2699
|
+
* | | Mo | 1st, 2nd, ..., 12th | 7 |
|
|
2700
|
+
* | | MM | 01, 02, ..., 12 | |
|
|
2701
|
+
* | | MMM | Jan, Feb, ..., Dec | |
|
|
2702
|
+
* | | MMMM | January, February, ..., December | 2 |
|
|
2703
|
+
* | | MMMMM | J, F, ..., D | |
|
|
2704
|
+
* | Month (stand-alone) | L | 1, 2, ..., 12 | |
|
|
2705
|
+
* | | Lo | 1st, 2nd, ..., 12th | 7 |
|
|
2706
|
+
* | | LL | 01, 02, ..., 12 | |
|
|
2707
|
+
* | | LLL | Jan, Feb, ..., Dec | |
|
|
2708
|
+
* | | LLLL | January, February, ..., December | 2 |
|
|
2709
|
+
* | | LLLLL | J, F, ..., D | |
|
|
2710
|
+
* | Local week of year | w | 1, 2, ..., 53 | |
|
|
2711
|
+
* | | wo | 1st, 2nd, ..., 53th | 7 |
|
|
2712
|
+
* | | ww | 01, 02, ..., 53 | |
|
|
2713
|
+
* | ISO week of year | I | 1, 2, ..., 53 | 7 |
|
|
2714
|
+
* | | Io | 1st, 2nd, ..., 53th | 7 |
|
|
2715
|
+
* | | II | 01, 02, ..., 53 | 7 |
|
|
2716
|
+
* | Day of month | d | 1, 2, ..., 31 | |
|
|
2717
|
+
* | | do | 1st, 2nd, ..., 31st | 7 |
|
|
2718
|
+
* | | dd | 01, 02, ..., 31 | |
|
|
2719
|
+
* | Day of year | D | 1, 2, ..., 365, 366 | 9 |
|
|
2720
|
+
* | | Do | 1st, 2nd, ..., 365th, 366th | 7 |
|
|
2721
|
+
* | | DD | 01, 02, ..., 365, 366 | 9 |
|
|
2722
|
+
* | | DDD | 001, 002, ..., 365, 366 | |
|
|
2723
|
+
* | | DDDD | ... | 3 |
|
|
2724
|
+
* | Day of week (formatting) | E..EEE | Mon, Tue, Wed, ..., Sun | |
|
|
2725
|
+
* | | EEEE | Monday, Tuesday, ..., Sunday | 2 |
|
|
2726
|
+
* | | EEEEE | M, T, W, T, F, S, S | |
|
|
2727
|
+
* | | EEEEEE | Mo, Tu, We, Th, Fr, Sa, Su | |
|
|
2728
|
+
* | ISO day of week (formatting) | i | 1, 2, 3, ..., 7 | 7 |
|
|
2729
|
+
* | | io | 1st, 2nd, ..., 7th | 7 |
|
|
2730
|
+
* | | ii | 01, 02, ..., 07 | 7 |
|
|
2731
|
+
* | | iii | Mon, Tue, Wed, ..., Sun | 7 |
|
|
2732
|
+
* | | iiii | Monday, Tuesday, ..., Sunday | 2,7 |
|
|
2733
|
+
* | | iiiii | M, T, W, T, F, S, S | 7 |
|
|
2734
|
+
* | | iiiiii | Mo, Tu, We, Th, Fr, Sa, Su | 7 |
|
|
2735
|
+
* | Local day of week (formatting) | e | 2, 3, 4, ..., 1 | |
|
|
2736
|
+
* | | eo | 2nd, 3rd, ..., 1st | 7 |
|
|
2737
|
+
* | | ee | 02, 03, ..., 01 | |
|
|
2738
|
+
* | | eee | Mon, Tue, Wed, ..., Sun | |
|
|
2739
|
+
* | | eeee | Monday, Tuesday, ..., Sunday | 2 |
|
|
2740
|
+
* | | eeeee | M, T, W, T, F, S, S | |
|
|
2741
|
+
* | | eeeeee | Mo, Tu, We, Th, Fr, Sa, Su | |
|
|
2742
|
+
* | Local day of week (stand-alone) | c | 2, 3, 4, ..., 1 | |
|
|
2743
|
+
* | | co | 2nd, 3rd, ..., 1st | 7 |
|
|
2744
|
+
* | | cc | 02, 03, ..., 01 | |
|
|
2745
|
+
* | | ccc | Mon, Tue, Wed, ..., Sun | |
|
|
2746
|
+
* | | cccc | Monday, Tuesday, ..., Sunday | 2 |
|
|
2747
|
+
* | | ccccc | M, T, W, T, F, S, S | |
|
|
2748
|
+
* | | cccccc | Mo, Tu, We, Th, Fr, Sa, Su | |
|
|
2749
|
+
* | AM, PM | a..aa | AM, PM | |
|
|
2750
|
+
* | | aaa | am, pm | |
|
|
2751
|
+
* | | aaaa | a.m., p.m. | 2 |
|
|
2752
|
+
* | | aaaaa | a, p | |
|
|
2753
|
+
* | AM, PM, noon, midnight | b..bb | AM, PM, noon, midnight | |
|
|
2754
|
+
* | | bbb | am, pm, noon, midnight | |
|
|
2755
|
+
* | | bbbb | a.m., p.m., noon, midnight | 2 |
|
|
2756
|
+
* | | bbbbb | a, p, n, mi | |
|
|
2757
|
+
* | Flexible day period | B..BBB | at night, in the morning, ... | |
|
|
2758
|
+
* | | BBBB | at night, in the morning, ... | 2 |
|
|
2759
|
+
* | | BBBBB | at night, in the morning, ... | |
|
|
2760
|
+
* | Hour [1-12] | h | 1, 2, ..., 11, 12 | |
|
|
2761
|
+
* | | ho | 1st, 2nd, ..., 11th, 12th | 7 |
|
|
2762
|
+
* | | hh | 01, 02, ..., 11, 12 | |
|
|
2763
|
+
* | Hour [0-23] | H | 0, 1, 2, ..., 23 | |
|
|
2764
|
+
* | | Ho | 0th, 1st, 2nd, ..., 23rd | 7 |
|
|
2765
|
+
* | | HH | 00, 01, 02, ..., 23 | |
|
|
2766
|
+
* | Hour [0-11] | K | 1, 2, ..., 11, 0 | |
|
|
2767
|
+
* | | Ko | 1st, 2nd, ..., 11th, 0th | 7 |
|
|
2768
|
+
* | | KK | 01, 02, ..., 11, 00 | |
|
|
2769
|
+
* | Hour [1-24] | k | 24, 1, 2, ..., 23 | |
|
|
2770
|
+
* | | ko | 24th, 1st, 2nd, ..., 23rd | 7 |
|
|
2771
|
+
* | | kk | 24, 01, 02, ..., 23 | |
|
|
2772
|
+
* | Minute | m | 0, 1, ..., 59 | |
|
|
2773
|
+
* | | mo | 0th, 1st, ..., 59th | 7 |
|
|
2774
|
+
* | | mm | 00, 01, ..., 59 | |
|
|
2775
|
+
* | Second | s | 0, 1, ..., 59 | |
|
|
2776
|
+
* | | so | 0th, 1st, ..., 59th | 7 |
|
|
2777
|
+
* | | ss | 00, 01, ..., 59 | |
|
|
2778
|
+
* | Fraction of second | S | 0, 1, ..., 9 | |
|
|
2779
|
+
* | | SS | 00, 01, ..., 99 | |
|
|
2780
|
+
* | | SSS | 000, 001, ..., 999 | |
|
|
2781
|
+
* | | SSSS | ... | 3 |
|
|
2782
|
+
* | Timezone (ISO-8601 w/ Z) | X | -08, +0530, Z | |
|
|
2783
|
+
* | | XX | -0800, +0530, Z | |
|
|
2784
|
+
* | | XXX | -08:00, +05:30, Z | |
|
|
2785
|
+
* | | XXXX | -0800, +0530, Z, +123456 | 2 |
|
|
2786
|
+
* | | XXXXX | -08:00, +05:30, Z, +12:34:56 | |
|
|
2787
|
+
* | Timezone (ISO-8601 w/o Z) | x | -08, +0530, +00 | |
|
|
2788
|
+
* | | xx | -0800, +0530, +0000 | |
|
|
2789
|
+
* | | xxx | -08:00, +05:30, +00:00 | 2 |
|
|
2790
|
+
* | | xxxx | -0800, +0530, +0000, +123456 | |
|
|
2791
|
+
* | | xxxxx | -08:00, +05:30, +00:00, +12:34:56 | |
|
|
2792
|
+
* | Timezone (GMT) | O...OOO | GMT-8, GMT+5:30, GMT+0 | |
|
|
2793
|
+
* | | OOOO | GMT-08:00, GMT+05:30, GMT+00:00 | 2 |
|
|
2794
|
+
* | Timezone (specific non-locat.) | z...zzz | GMT-8, GMT+5:30, GMT+0 | 6 |
|
|
2795
|
+
* | | zzzz | GMT-08:00, GMT+05:30, GMT+00:00 | 2,6 |
|
|
2796
|
+
* | Seconds timestamp | t | 512969520 | 7 |
|
|
2797
|
+
* | | tt | ... | 3,7 |
|
|
2798
|
+
* | Milliseconds timestamp | T | 512969520900 | 7 |
|
|
2799
|
+
* | | TT | ... | 3,7 |
|
|
2800
|
+
* | Long localized date | P | 04/29/1453 | 7 |
|
|
2801
|
+
* | | PP | Apr 29, 1453 | 7 |
|
|
2802
|
+
* | | PPP | April 29th, 1453 | 7 |
|
|
2803
|
+
* | | PPPP | Friday, April 29th, 1453 | 2,7 |
|
|
2804
|
+
* | Long localized time | p | 12:00 AM | 7 |
|
|
2805
|
+
* | | pp | 12:00:00 AM | 7 |
|
|
2806
|
+
* | | ppp | 12:00:00 AM GMT+2 | 7 |
|
|
2807
|
+
* | | pppp | 12:00:00 AM GMT+02:00 | 2,7 |
|
|
2808
|
+
* | Combination of date and time | Pp | 04/29/1453, 12:00 AM | 7 |
|
|
2809
|
+
* | | PPpp | Apr 29, 1453, 12:00:00 AM | 7 |
|
|
2810
|
+
* | | PPPppp | April 29th, 1453 at ... | 7 |
|
|
2811
|
+
* | | PPPPpppp| Friday, April 29th, 1453 at ... | 2,7 |
|
|
2812
|
+
* Notes:
|
|
2813
|
+
* 1. "Formatting" units (e.g. formatting quarter) in the default en-US locale
|
|
2814
|
+
* are the same as "stand-alone" units, but are different in some languages.
|
|
2815
|
+
* "Formatting" units are declined according to the rules of the language
|
|
2816
|
+
* in the context of a date. "Stand-alone" units are always nominative singular:
|
|
2817
|
+
*
|
|
2818
|
+
* `format(new Date(2017, 10, 6), 'do LLLL', {locale: cs}) //=> '6. listopad'`
|
|
2819
|
+
*
|
|
2820
|
+
* `format(new Date(2017, 10, 6), 'do MMMM', {locale: cs}) //=> '6. listopadu'`
|
|
2821
|
+
*
|
|
2822
|
+
* 2. Any sequence of the identical letters is a pattern, unless it is escaped by
|
|
2823
|
+
* the single quote characters (see below).
|
|
2824
|
+
* If the sequence is longer than listed in table (e.g. `EEEEEEEEEEE`)
|
|
2825
|
+
* the output will be the same as default pattern for this unit, usually
|
|
2826
|
+
* the longest one (in case of ISO weekdays, `EEEE`). Default patterns for units
|
|
2827
|
+
* are marked with "2" in the last column of the table.
|
|
2828
|
+
*
|
|
2829
|
+
* `format(new Date(2017, 10, 6), 'MMM') //=> 'Nov'`
|
|
2830
|
+
*
|
|
2831
|
+
* `format(new Date(2017, 10, 6), 'MMMM') //=> 'November'`
|
|
2832
|
+
*
|
|
2833
|
+
* `format(new Date(2017, 10, 6), 'MMMMM') //=> 'N'`
|
|
2834
|
+
*
|
|
2835
|
+
* `format(new Date(2017, 10, 6), 'MMMMMM') //=> 'November'`
|
|
2836
|
+
*
|
|
2837
|
+
* `format(new Date(2017, 10, 6), 'MMMMMMM') //=> 'November'`
|
|
2838
|
+
*
|
|
2839
|
+
* 3. Some patterns could be unlimited length (such as `yyyyyyyy`).
|
|
2840
|
+
* The output will be padded with zeros to match the length of the pattern.
|
|
2841
|
+
*
|
|
2842
|
+
* `format(new Date(2017, 10, 6), 'yyyyyyyy') //=> '00002017'`
|
|
2843
|
+
*
|
|
2844
|
+
* 4. `QQQQQ` and `qqqqq` could be not strictly numerical in some locales.
|
|
2845
|
+
* These tokens represent the shortest form of the quarter.
|
|
2846
|
+
*
|
|
2847
|
+
* 5. The main difference between `y` and `u` patterns are B.C. years:
|
|
2848
|
+
*
|
|
2849
|
+
* | Year | `y` | `u` |
|
|
2850
|
+
* |------|-----|-----|
|
|
2851
|
+
* | AC 1 | 1 | 1 |
|
|
2852
|
+
* | BC 1 | 1 | 0 |
|
|
2853
|
+
* | BC 2 | 2 | -1 |
|
|
2854
|
+
*
|
|
2855
|
+
* Also `yy` always returns the last two digits of a year,
|
|
2856
|
+
* while `uu` pads single digit years to 2 characters and returns other years unchanged:
|
|
2857
|
+
*
|
|
2858
|
+
* | Year | `yy` | `uu` |
|
|
2859
|
+
* |------|------|------|
|
|
2860
|
+
* | 1 | 01 | 01 |
|
|
2861
|
+
* | 14 | 14 | 14 |
|
|
2862
|
+
* | 376 | 76 | 376 |
|
|
2863
|
+
* | 1453 | 53 | 1453 |
|
|
2864
|
+
*
|
|
2865
|
+
* The same difference is true for local and ISO week-numbering years (`Y` and `R`),
|
|
2866
|
+
* except local week-numbering years are dependent on `options.weekStartsOn`
|
|
2867
|
+
* and `options.firstWeekContainsDate` (compare [getISOWeekYear](https://date-fns.org/docs/getISOWeekYear)
|
|
2868
|
+
* and [getWeekYear](https://date-fns.org/docs/getWeekYear)).
|
|
2869
|
+
*
|
|
2870
|
+
* 6. Specific non-location timezones are currently unavailable in `date-fns`,
|
|
2871
|
+
* so right now these tokens fall back to GMT timezones.
|
|
2872
|
+
*
|
|
2873
|
+
* 7. These patterns are not in the Unicode Technical Standard #35:
|
|
2874
|
+
* - `i`: ISO day of week
|
|
2875
|
+
* - `I`: ISO week of year
|
|
2876
|
+
* - `R`: ISO week-numbering year
|
|
2877
|
+
* - `t`: seconds timestamp
|
|
2878
|
+
* - `T`: milliseconds timestamp
|
|
2879
|
+
* - `o`: ordinal number modifier
|
|
2880
|
+
* - `P`: long localized date
|
|
2881
|
+
* - `p`: long localized time
|
|
2882
|
+
*
|
|
2883
|
+
* 8. `YY` and `YYYY` tokens represent week-numbering years but they are often confused with years.
|
|
2884
|
+
* You should enable `options.useAdditionalWeekYearTokens` to use them. See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
|
|
2885
|
+
*
|
|
2886
|
+
* 9. `D` and `DD` tokens represent days of the year but they are often confused with days of the month.
|
|
2887
|
+
* You should enable `options.useAdditionalDayOfYearTokens` to use them. See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
|
|
2888
|
+
*
|
|
2889
|
+
* @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
|
|
2890
|
+
*
|
|
2891
|
+
* @param date - The original date
|
|
2892
|
+
* @param format - The string of tokens
|
|
2893
|
+
* @param options - An object with options
|
|
2894
|
+
*
|
|
2895
|
+
* @returns The formatted date string
|
|
2896
|
+
*
|
|
2897
|
+
* @throws `date` must not be Invalid Date
|
|
2898
|
+
* @throws `options.locale` must contain `localize` property
|
|
2899
|
+
* @throws `options.locale` must contain `formatLong` property
|
|
2900
|
+
* @throws use `yyyy` instead of `YYYY` for formatting years using [format provided] to the input [input provided]; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
|
|
2901
|
+
* @throws use `yy` instead of `YY` for formatting years using [format provided] to the input [input provided]; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
|
|
2902
|
+
* @throws use `d` instead of `D` for formatting days of the month using [format provided] to the input [input provided]; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
|
|
2903
|
+
* @throws use `dd` instead of `DD` for formatting days of the month using [format provided] to the input [input provided]; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
|
|
2904
|
+
* @throws format string contains an unescaped latin alphabet character
|
|
2905
|
+
*
|
|
2906
|
+
* @example
|
|
2907
|
+
* // Represent 11 February 2014 in middle-endian format:
|
|
2908
|
+
* const result = format(new Date(2014, 1, 11), 'MM/dd/yyyy')
|
|
2909
|
+
* //=> '02/11/2014'
|
|
2910
|
+
*
|
|
2911
|
+
* @example
|
|
2912
|
+
* // Represent 2 July 2014 in Esperanto:
|
|
2913
|
+
* import { eoLocale } from 'date-fns/locale/eo'
|
|
2914
|
+
* const result = format(new Date(2014, 6, 2), "do 'de' MMMM yyyy", {
|
|
2915
|
+
* locale: eoLocale
|
|
2916
|
+
* })
|
|
2917
|
+
* //=> '2-a de julio 2014'
|
|
2918
|
+
*
|
|
2919
|
+
* @example
|
|
2920
|
+
* // Escape string by single quote characters:
|
|
2921
|
+
* const result = format(new Date(2014, 6, 2, 15), "h 'o''clock'")
|
|
2922
|
+
* //=> "3 o'clock"
|
|
2923
|
+
*/
|
|
2924
|
+
function format(date, formatStr, options) {
|
|
2925
|
+
const defaultOptions = getDefaultOptions();
|
|
2926
|
+
const locale = options?.locale ?? defaultOptions.locale ?? enUS;
|
|
2927
|
+
|
|
2928
|
+
const firstWeekContainsDate =
|
|
2929
|
+
options?.firstWeekContainsDate ??
|
|
2930
|
+
options?.locale?.options?.firstWeekContainsDate ??
|
|
2931
|
+
defaultOptions.firstWeekContainsDate ??
|
|
2932
|
+
defaultOptions.locale?.options?.firstWeekContainsDate ??
|
|
2933
|
+
1;
|
|
2934
|
+
|
|
2935
|
+
const weekStartsOn =
|
|
2936
|
+
options?.weekStartsOn ??
|
|
2937
|
+
options?.locale?.options?.weekStartsOn ??
|
|
2938
|
+
defaultOptions.weekStartsOn ??
|
|
2939
|
+
defaultOptions.locale?.options?.weekStartsOn ??
|
|
2940
|
+
0;
|
|
2941
|
+
|
|
2942
|
+
const originalDate = toDate(date);
|
|
2943
|
+
|
|
2944
|
+
if (!isValid(originalDate)) {
|
|
2945
|
+
throw new RangeError("Invalid time value");
|
|
2946
|
+
}
|
|
2947
|
+
|
|
2948
|
+
let parts = formatStr
|
|
2949
|
+
.match(longFormattingTokensRegExp)
|
|
2950
|
+
.map((substring) => {
|
|
2951
|
+
const firstCharacter = substring[0];
|
|
2952
|
+
if (firstCharacter === "p" || firstCharacter === "P") {
|
|
2953
|
+
const longFormatter = longFormatters[firstCharacter];
|
|
2954
|
+
return longFormatter(substring, locale.formatLong);
|
|
2955
|
+
}
|
|
2956
|
+
return substring;
|
|
2957
|
+
})
|
|
2958
|
+
.join("")
|
|
2959
|
+
.match(formattingTokensRegExp)
|
|
2960
|
+
.map((substring) => {
|
|
2961
|
+
// Replace two single quote characters with one single quote character
|
|
2962
|
+
if (substring === "''") {
|
|
2963
|
+
return { isToken: false, value: "'" };
|
|
2964
|
+
}
|
|
2965
|
+
|
|
2966
|
+
const firstCharacter = substring[0];
|
|
2967
|
+
if (firstCharacter === "'") {
|
|
2968
|
+
return { isToken: false, value: cleanEscapedString(substring) };
|
|
2969
|
+
}
|
|
2970
|
+
|
|
2971
|
+
if (formatters[firstCharacter]) {
|
|
2972
|
+
return { isToken: true, value: substring };
|
|
2973
|
+
}
|
|
2974
|
+
|
|
2975
|
+
if (firstCharacter.match(unescapedLatinCharacterRegExp)) {
|
|
2976
|
+
throw new RangeError(
|
|
2977
|
+
"Format string contains an unescaped latin alphabet character `" +
|
|
2978
|
+
firstCharacter +
|
|
2979
|
+
"`",
|
|
2980
|
+
);
|
|
2981
|
+
}
|
|
2982
|
+
|
|
2983
|
+
return { isToken: false, value: substring };
|
|
2984
|
+
});
|
|
2985
|
+
|
|
2986
|
+
// invoke localize preprocessor (only for french locales at the moment)
|
|
2987
|
+
if (locale.localize.preprocessor) {
|
|
2988
|
+
parts = locale.localize.preprocessor(originalDate, parts);
|
|
2989
|
+
}
|
|
2990
|
+
|
|
2991
|
+
const formatterOptions = {
|
|
2992
|
+
firstWeekContainsDate,
|
|
2993
|
+
weekStartsOn,
|
|
2994
|
+
locale,
|
|
2995
|
+
};
|
|
2996
|
+
|
|
2997
|
+
return parts
|
|
2998
|
+
.map((part) => {
|
|
2999
|
+
if (!part.isToken) return part.value;
|
|
3000
|
+
|
|
3001
|
+
const token = part.value;
|
|
3002
|
+
|
|
3003
|
+
if (
|
|
3004
|
+
(!options?.useAdditionalWeekYearTokens &&
|
|
3005
|
+
isProtectedWeekYearToken(token)) ||
|
|
3006
|
+
(!options?.useAdditionalDayOfYearTokens &&
|
|
3007
|
+
isProtectedDayOfYearToken(token))
|
|
3008
|
+
) {
|
|
3009
|
+
warnOrThrowProtectedError(token, formatStr, String(date));
|
|
3010
|
+
}
|
|
3011
|
+
|
|
3012
|
+
const formatter = formatters[token[0]];
|
|
3013
|
+
return formatter(originalDate, token, locale.localize, formatterOptions);
|
|
3014
|
+
})
|
|
3015
|
+
.join("");
|
|
3016
|
+
}
|
|
3017
|
+
|
|
3018
|
+
function cleanEscapedString(input) {
|
|
3019
|
+
const matched = input.match(escapedStringRegExp);
|
|
3020
|
+
|
|
3021
|
+
if (!matched) {
|
|
3022
|
+
return input;
|
|
3023
|
+
}
|
|
3024
|
+
|
|
3025
|
+
return matched[1].replace(doubleQuoteRegExp, "'");
|
|
3026
|
+
}
|
|
3027
|
+
|
|
3028
|
+
/**
|
|
3029
|
+
* @name isSameMonth
|
|
3030
|
+
* @category Month Helpers
|
|
3031
|
+
* @summary Are the given dates in the same month (and year)?
|
|
3032
|
+
*
|
|
3033
|
+
* @description
|
|
3034
|
+
* Are the given dates in the same month (and year)?
|
|
3035
|
+
*
|
|
3036
|
+
* @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
|
|
3037
|
+
*
|
|
3038
|
+
* @param dateLeft - The first date to check
|
|
3039
|
+
* @param dateRight - The second date to check
|
|
3040
|
+
*
|
|
3041
|
+
* @returns The dates are in the same month (and year)
|
|
3042
|
+
*
|
|
3043
|
+
* @example
|
|
3044
|
+
* // Are 2 September 2014 and 25 September 2014 in the same month?
|
|
3045
|
+
* const result = isSameMonth(new Date(2014, 8, 2), new Date(2014, 8, 25))
|
|
3046
|
+
* //=> true
|
|
3047
|
+
*
|
|
3048
|
+
* @example
|
|
3049
|
+
* // Are 2 September 2014 and 25 September 2015 in the same month?
|
|
3050
|
+
* const result = isSameMonth(new Date(2014, 8, 2), new Date(2015, 8, 25))
|
|
3051
|
+
* //=> false
|
|
3052
|
+
*/
|
|
3053
|
+
function isSameMonth(dateLeft, dateRight) {
|
|
3054
|
+
const _dateLeft = toDate(dateLeft);
|
|
3055
|
+
const _dateRight = toDate(dateRight);
|
|
3056
|
+
return (
|
|
3057
|
+
_dateLeft.getFullYear() === _dateRight.getFullYear() &&
|
|
3058
|
+
_dateLeft.getMonth() === _dateRight.getMonth()
|
|
3059
|
+
);
|
|
3060
|
+
}
|
|
3061
|
+
|
|
3062
|
+
/**
|
|
3063
|
+
* The {@link parseISO} function options.
|
|
3064
|
+
*/
|
|
3065
|
+
|
|
3066
|
+
/**
|
|
3067
|
+
* @name parseISO
|
|
3068
|
+
* @category Common Helpers
|
|
3069
|
+
* @summary Parse ISO string
|
|
3070
|
+
*
|
|
3071
|
+
* @description
|
|
3072
|
+
* Parse the given string in ISO 8601 format and return an instance of Date.
|
|
3073
|
+
*
|
|
3074
|
+
* Function accepts complete ISO 8601 formats as well as partial implementations.
|
|
3075
|
+
* ISO 8601: http://en.wikipedia.org/wiki/ISO_8601
|
|
3076
|
+
*
|
|
3077
|
+
* If the argument isn't a string, the function cannot parse the string or
|
|
3078
|
+
* the values are invalid, it returns Invalid Date.
|
|
3079
|
+
*
|
|
3080
|
+
* @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
|
|
3081
|
+
*
|
|
3082
|
+
* @param argument - The value to convert
|
|
3083
|
+
* @param options - An object with options
|
|
3084
|
+
*
|
|
3085
|
+
* @returns The parsed date in the local time zone
|
|
3086
|
+
*
|
|
3087
|
+
* @example
|
|
3088
|
+
* // Convert string '2014-02-11T11:30:30' to date:
|
|
3089
|
+
* const result = parseISO('2014-02-11T11:30:30')
|
|
3090
|
+
* //=> Tue Feb 11 2014 11:30:30
|
|
3091
|
+
*
|
|
3092
|
+
* @example
|
|
3093
|
+
* // Convert string '+02014101' to date,
|
|
3094
|
+
* // if the additional number of digits in the extended year format is 1:
|
|
3095
|
+
* const result = parseISO('+02014101', { additionalDigits: 1 })
|
|
3096
|
+
* //=> Fri Apr 11 2014 00:00:00
|
|
3097
|
+
*/
|
|
3098
|
+
function parseISO(argument, options) {
|
|
3099
|
+
const additionalDigits = options?.additionalDigits ?? 2;
|
|
3100
|
+
const dateStrings = splitDateString(argument);
|
|
3101
|
+
|
|
3102
|
+
let date;
|
|
3103
|
+
if (dateStrings.date) {
|
|
3104
|
+
const parseYearResult = parseYear(dateStrings.date, additionalDigits);
|
|
3105
|
+
date = parseDate(parseYearResult.restDateString, parseYearResult.year);
|
|
3106
|
+
}
|
|
3107
|
+
|
|
3108
|
+
if (!date || isNaN(date.getTime())) {
|
|
3109
|
+
return new Date(NaN);
|
|
3110
|
+
}
|
|
3111
|
+
|
|
3112
|
+
const timestamp = date.getTime();
|
|
3113
|
+
let time = 0;
|
|
3114
|
+
let offset;
|
|
3115
|
+
|
|
3116
|
+
if (dateStrings.time) {
|
|
3117
|
+
time = parseTime(dateStrings.time);
|
|
3118
|
+
if (isNaN(time)) {
|
|
3119
|
+
return new Date(NaN);
|
|
3120
|
+
}
|
|
3121
|
+
}
|
|
3122
|
+
|
|
3123
|
+
if (dateStrings.timezone) {
|
|
3124
|
+
offset = parseTimezone(dateStrings.timezone);
|
|
3125
|
+
if (isNaN(offset)) {
|
|
3126
|
+
return new Date(NaN);
|
|
3127
|
+
}
|
|
3128
|
+
} else {
|
|
3129
|
+
const dirtyDate = new Date(timestamp + time);
|
|
3130
|
+
// JS parsed string assuming it's in UTC timezone
|
|
3131
|
+
// but we need it to be parsed in our timezone
|
|
3132
|
+
// so we use utc values to build date in our timezone.
|
|
3133
|
+
// Year values from 0 to 99 map to the years 1900 to 1999
|
|
3134
|
+
// so set year explicitly with setFullYear.
|
|
3135
|
+
const result = new Date(0);
|
|
3136
|
+
result.setFullYear(
|
|
3137
|
+
dirtyDate.getUTCFullYear(),
|
|
3138
|
+
dirtyDate.getUTCMonth(),
|
|
3139
|
+
dirtyDate.getUTCDate(),
|
|
3140
|
+
);
|
|
3141
|
+
result.setHours(
|
|
3142
|
+
dirtyDate.getUTCHours(),
|
|
3143
|
+
dirtyDate.getUTCMinutes(),
|
|
3144
|
+
dirtyDate.getUTCSeconds(),
|
|
3145
|
+
dirtyDate.getUTCMilliseconds(),
|
|
3146
|
+
);
|
|
3147
|
+
return result;
|
|
3148
|
+
}
|
|
3149
|
+
|
|
3150
|
+
return new Date(timestamp + time + offset);
|
|
3151
|
+
}
|
|
3152
|
+
|
|
3153
|
+
const patterns = {
|
|
3154
|
+
dateTimeDelimiter: /[T ]/,
|
|
3155
|
+
timeZoneDelimiter: /[Z ]/i,
|
|
3156
|
+
timezone: /([Z+-].*)$/,
|
|
3157
|
+
};
|
|
3158
|
+
|
|
3159
|
+
const dateRegex =
|
|
3160
|
+
/^-?(?:(\d{3})|(\d{2})(?:-?(\d{2}))?|W(\d{2})(?:-?(\d{1}))?|)$/;
|
|
3161
|
+
const timeRegex =
|
|
3162
|
+
/^(\d{2}(?:[.,]\d*)?)(?::?(\d{2}(?:[.,]\d*)?))?(?::?(\d{2}(?:[.,]\d*)?))?$/;
|
|
3163
|
+
const timezoneRegex = /^([+-])(\d{2})(?::?(\d{2}))?$/;
|
|
3164
|
+
|
|
3165
|
+
function splitDateString(dateString) {
|
|
3166
|
+
const dateStrings = {};
|
|
3167
|
+
const array = dateString.split(patterns.dateTimeDelimiter);
|
|
3168
|
+
let timeString;
|
|
3169
|
+
|
|
3170
|
+
// The regex match should only return at maximum two array elements.
|
|
3171
|
+
// [date], [time], or [date, time].
|
|
3172
|
+
if (array.length > 2) {
|
|
3173
|
+
return dateStrings;
|
|
3174
|
+
}
|
|
3175
|
+
|
|
3176
|
+
if (/:/.test(array[0])) {
|
|
3177
|
+
timeString = array[0];
|
|
3178
|
+
} else {
|
|
3179
|
+
dateStrings.date = array[0];
|
|
3180
|
+
timeString = array[1];
|
|
3181
|
+
if (patterns.timeZoneDelimiter.test(dateStrings.date)) {
|
|
3182
|
+
dateStrings.date = dateString.split(patterns.timeZoneDelimiter)[0];
|
|
3183
|
+
timeString = dateString.substr(
|
|
3184
|
+
dateStrings.date.length,
|
|
3185
|
+
dateString.length,
|
|
3186
|
+
);
|
|
3187
|
+
}
|
|
3188
|
+
}
|
|
3189
|
+
|
|
3190
|
+
if (timeString) {
|
|
3191
|
+
const token = patterns.timezone.exec(timeString);
|
|
3192
|
+
if (token) {
|
|
3193
|
+
dateStrings.time = timeString.replace(token[1], "");
|
|
3194
|
+
dateStrings.timezone = token[1];
|
|
3195
|
+
} else {
|
|
3196
|
+
dateStrings.time = timeString;
|
|
3197
|
+
}
|
|
3198
|
+
}
|
|
3199
|
+
|
|
3200
|
+
return dateStrings;
|
|
3201
|
+
}
|
|
3202
|
+
|
|
3203
|
+
function parseYear(dateString, additionalDigits) {
|
|
3204
|
+
const regex = new RegExp(
|
|
3205
|
+
"^(?:(\\d{4}|[+-]\\d{" +
|
|
3206
|
+
(4 + additionalDigits) +
|
|
3207
|
+
"})|(\\d{2}|[+-]\\d{" +
|
|
3208
|
+
(2 + additionalDigits) +
|
|
3209
|
+
"})$)",
|
|
3210
|
+
);
|
|
3211
|
+
|
|
3212
|
+
const captures = dateString.match(regex);
|
|
3213
|
+
// Invalid ISO-formatted year
|
|
3214
|
+
if (!captures) return { year: NaN, restDateString: "" };
|
|
3215
|
+
|
|
3216
|
+
const year = captures[1] ? parseInt(captures[1]) : null;
|
|
3217
|
+
const century = captures[2] ? parseInt(captures[2]) : null;
|
|
3218
|
+
|
|
3219
|
+
// either year or century is null, not both
|
|
3220
|
+
return {
|
|
3221
|
+
year: century === null ? year : century * 100,
|
|
3222
|
+
restDateString: dateString.slice((captures[1] || captures[2]).length),
|
|
3223
|
+
};
|
|
3224
|
+
}
|
|
3225
|
+
|
|
3226
|
+
function parseDate(dateString, year) {
|
|
3227
|
+
// Invalid ISO-formatted year
|
|
3228
|
+
if (year === null) return new Date(NaN);
|
|
3229
|
+
|
|
3230
|
+
const captures = dateString.match(dateRegex);
|
|
3231
|
+
// Invalid ISO-formatted string
|
|
3232
|
+
if (!captures) return new Date(NaN);
|
|
3233
|
+
|
|
3234
|
+
const isWeekDate = !!captures[4];
|
|
3235
|
+
const dayOfYear = parseDateUnit(captures[1]);
|
|
3236
|
+
const month = parseDateUnit(captures[2]) - 1;
|
|
3237
|
+
const day = parseDateUnit(captures[3]);
|
|
3238
|
+
const week = parseDateUnit(captures[4]);
|
|
3239
|
+
const dayOfWeek = parseDateUnit(captures[5]) - 1;
|
|
3240
|
+
|
|
3241
|
+
if (isWeekDate) {
|
|
3242
|
+
if (!validateWeekDate(year, week, dayOfWeek)) {
|
|
3243
|
+
return new Date(NaN);
|
|
3244
|
+
}
|
|
3245
|
+
return dayOfISOWeekYear(year, week, dayOfWeek);
|
|
3246
|
+
} else {
|
|
3247
|
+
const date = new Date(0);
|
|
3248
|
+
if (
|
|
3249
|
+
!validateDate(year, month, day) ||
|
|
3250
|
+
!validateDayOfYearDate(year, dayOfYear)
|
|
3251
|
+
) {
|
|
3252
|
+
return new Date(NaN);
|
|
3253
|
+
}
|
|
3254
|
+
date.setUTCFullYear(year, month, Math.max(dayOfYear, day));
|
|
3255
|
+
return date;
|
|
3256
|
+
}
|
|
3257
|
+
}
|
|
3258
|
+
|
|
3259
|
+
function parseDateUnit(value) {
|
|
3260
|
+
return value ? parseInt(value) : 1;
|
|
3261
|
+
}
|
|
3262
|
+
|
|
3263
|
+
function parseTime(timeString) {
|
|
3264
|
+
const captures = timeString.match(timeRegex);
|
|
3265
|
+
if (!captures) return NaN; // Invalid ISO-formatted time
|
|
3266
|
+
|
|
3267
|
+
const hours = parseTimeUnit(captures[1]);
|
|
3268
|
+
const minutes = parseTimeUnit(captures[2]);
|
|
3269
|
+
const seconds = parseTimeUnit(captures[3]);
|
|
3270
|
+
|
|
3271
|
+
if (!validateTime(hours, minutes, seconds)) {
|
|
3272
|
+
return NaN;
|
|
3273
|
+
}
|
|
3274
|
+
|
|
3275
|
+
return (
|
|
3276
|
+
hours * millisecondsInHour + minutes * millisecondsInMinute + seconds * 1000
|
|
3277
|
+
);
|
|
3278
|
+
}
|
|
3279
|
+
|
|
3280
|
+
function parseTimeUnit(value) {
|
|
3281
|
+
return (value && parseFloat(value.replace(",", "."))) || 0;
|
|
3282
|
+
}
|
|
3283
|
+
|
|
3284
|
+
function parseTimezone(timezoneString) {
|
|
3285
|
+
if (timezoneString === "Z") return 0;
|
|
3286
|
+
|
|
3287
|
+
const captures = timezoneString.match(timezoneRegex);
|
|
3288
|
+
if (!captures) return 0;
|
|
3289
|
+
|
|
3290
|
+
const sign = captures[1] === "+" ? -1 : 1;
|
|
3291
|
+
const hours = parseInt(captures[2]);
|
|
3292
|
+
const minutes = (captures[3] && parseInt(captures[3])) || 0;
|
|
3293
|
+
|
|
3294
|
+
if (!validateTimezone(hours, minutes)) {
|
|
3295
|
+
return NaN;
|
|
3296
|
+
}
|
|
3297
|
+
|
|
3298
|
+
return sign * (hours * millisecondsInHour + minutes * millisecondsInMinute);
|
|
3299
|
+
}
|
|
3300
|
+
|
|
3301
|
+
function dayOfISOWeekYear(isoWeekYear, week, day) {
|
|
3302
|
+
const date = new Date(0);
|
|
3303
|
+
date.setUTCFullYear(isoWeekYear, 0, 4);
|
|
3304
|
+
const fourthOfJanuaryDay = date.getUTCDay() || 7;
|
|
3305
|
+
const diff = (week - 1) * 7 + day + 1 - fourthOfJanuaryDay;
|
|
3306
|
+
date.setUTCDate(date.getUTCDate() + diff);
|
|
3307
|
+
return date;
|
|
3308
|
+
}
|
|
3309
|
+
|
|
3310
|
+
// Validation functions
|
|
3311
|
+
|
|
3312
|
+
// February is null to handle the leap year (using ||)
|
|
3313
|
+
const daysInMonths = [31, null, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
|
|
3314
|
+
|
|
3315
|
+
function isLeapYearIndex(year) {
|
|
3316
|
+
return year % 400 === 0 || (year % 4 === 0 && year % 100 !== 0);
|
|
3317
|
+
}
|
|
3318
|
+
|
|
3319
|
+
function validateDate(year, month, date) {
|
|
3320
|
+
return (
|
|
3321
|
+
month >= 0 &&
|
|
3322
|
+
month <= 11 &&
|
|
3323
|
+
date >= 1 &&
|
|
3324
|
+
date <= (daysInMonths[month] || (isLeapYearIndex(year) ? 29 : 28))
|
|
3325
|
+
);
|
|
3326
|
+
}
|
|
3327
|
+
|
|
3328
|
+
function validateDayOfYearDate(year, dayOfYear) {
|
|
3329
|
+
return dayOfYear >= 1 && dayOfYear <= (isLeapYearIndex(year) ? 366 : 365);
|
|
3330
|
+
}
|
|
3331
|
+
|
|
3332
|
+
function validateWeekDate(_year, week, day) {
|
|
3333
|
+
return week >= 1 && week <= 53 && day >= 0 && day <= 6;
|
|
3334
|
+
}
|
|
3335
|
+
|
|
3336
|
+
function validateTime(hours, minutes, seconds) {
|
|
3337
|
+
if (hours === 24) {
|
|
3338
|
+
return minutes === 0 && seconds === 0;
|
|
3339
|
+
}
|
|
3340
|
+
|
|
3341
|
+
return (
|
|
3342
|
+
seconds >= 0 &&
|
|
3343
|
+
seconds < 60 &&
|
|
3344
|
+
minutes >= 0 &&
|
|
3345
|
+
minutes < 60 &&
|
|
3346
|
+
hours >= 0 &&
|
|
3347
|
+
hours < 25
|
|
3348
|
+
);
|
|
3349
|
+
}
|
|
3350
|
+
|
|
3351
|
+
function validateTimezone(_hours, minutes) {
|
|
3352
|
+
return minutes >= 0 && minutes <= 59;
|
|
3353
|
+
}
|
|
3354
|
+
|
|
3355
|
+
/**
|
|
3356
|
+
* @name subMonths
|
|
3357
|
+
* @category Month Helpers
|
|
3358
|
+
* @summary Subtract the specified number of months from the given date.
|
|
3359
|
+
*
|
|
3360
|
+
* @description
|
|
3361
|
+
* Subtract the specified number of months from the given date.
|
|
3362
|
+
*
|
|
3363
|
+
* @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
|
|
3364
|
+
*
|
|
3365
|
+
* @param date - The date to be changed
|
|
3366
|
+
* @param amount - The amount of months to be subtracted.
|
|
3367
|
+
*
|
|
3368
|
+
* @returns The new date with the months subtracted
|
|
3369
|
+
*
|
|
3370
|
+
* @example
|
|
3371
|
+
* // Subtract 5 months from 1 February 2015:
|
|
3372
|
+
* const result = subMonths(new Date(2015, 1, 1), 5)
|
|
3373
|
+
* //=> Mon Sep 01 2014 00:00:00
|
|
3374
|
+
*/
|
|
3375
|
+
function subMonths(date, amount) {
|
|
3376
|
+
return addMonths(date, -amount);
|
|
3377
|
+
}
|
|
3378
|
+
|
|
3379
|
+
const getBaseUrl = () => {
|
|
3380
|
+
if (typeof process !== 'undefined' && process.env && process.env.REACT_APP_CALENDAR_API_URL) {
|
|
3381
|
+
return process.env.REACT_APP_CALENDAR_API_URL;
|
|
3382
|
+
}
|
|
3383
|
+
if (typeof window !== 'undefined' && window.CALENDAR_API_URL) {
|
|
3384
|
+
return window.CALENDAR_API_URL;
|
|
3385
|
+
}
|
|
3386
|
+
return 'http://localhost:8080';
|
|
3387
|
+
};
|
|
3388
|
+
const createBooking = (businessId, booking, apiBaseUrl) => __awaiter(void 0, void 0, void 0, function* () {
|
|
3389
|
+
const baseUrl = apiBaseUrl || getBaseUrl();
|
|
3390
|
+
const response = yield fetch(`${baseUrl}/businesses/${businessId}/bookings`, {
|
|
3391
|
+
method: 'POST',
|
|
3392
|
+
headers: {
|
|
3393
|
+
'Content-Type': 'application/json',
|
|
3394
|
+
},
|
|
3395
|
+
body: JSON.stringify(booking),
|
|
3396
|
+
});
|
|
3397
|
+
if (!response.ok) {
|
|
3398
|
+
const error = yield response.json();
|
|
3399
|
+
throw new Error(error.error || 'Failed to create booking');
|
|
3400
|
+
}
|
|
3401
|
+
return response.json();
|
|
3402
|
+
});
|
|
3403
|
+
const getResourceSchedule = (businessId, resourceId, startDate, endDate, apiBaseUrl) => __awaiter(void 0, void 0, void 0, function* () {
|
|
3404
|
+
const baseUrl = apiBaseUrl || getBaseUrl();
|
|
3405
|
+
const url = new URL(`${baseUrl}/businesses/${businessId}/schedules/${resourceId}`);
|
|
3406
|
+
url.searchParams.append('start_date', startDate);
|
|
3407
|
+
url.searchParams.append('end_date', endDate);
|
|
3408
|
+
const response = yield fetch(url.toString());
|
|
3409
|
+
if (!response.ok) {
|
|
3410
|
+
const error = yield response.json();
|
|
3411
|
+
throw new Error(error.error || 'Failed to fetch schedule');
|
|
3412
|
+
}
|
|
3413
|
+
return response.json();
|
|
3414
|
+
});
|
|
3415
|
+
const getAllBookings = (businessId, startDate, endDate, apiBaseUrl) => __awaiter(void 0, void 0, void 0, function* () {
|
|
3416
|
+
const baseUrl = apiBaseUrl || getBaseUrl();
|
|
3417
|
+
const url = new URL(`${baseUrl}/businesses/${businessId}/bookings`);
|
|
3418
|
+
url.searchParams.append('start_date', startDate);
|
|
3419
|
+
url.searchParams.append('end_date', endDate);
|
|
3420
|
+
const response = yield fetch(url.toString());
|
|
3421
|
+
if (!response.ok) {
|
|
3422
|
+
const error = yield response.json();
|
|
3423
|
+
throw new Error(error.error || 'Failed to fetch bookings');
|
|
3424
|
+
}
|
|
3425
|
+
return response.json();
|
|
3426
|
+
});
|
|
3427
|
+
const getMeetingDetails = (businessId, meetingId, apiBaseUrl) => __awaiter(void 0, void 0, void 0, function* () {
|
|
3428
|
+
const baseUrl = apiBaseUrl || getBaseUrl();
|
|
3429
|
+
const response = yield fetch(`${baseUrl}/businesses/${businessId}/meetings/${meetingId}`);
|
|
3430
|
+
if (!response.ok) {
|
|
3431
|
+
const error = yield response.json();
|
|
3432
|
+
throw new Error(error.error || 'Failed to fetch meeting details');
|
|
3433
|
+
}
|
|
3434
|
+
return response.json();
|
|
3435
|
+
});
|
|
3436
|
+
|
|
3437
|
+
function styleInject(css, ref) {
|
|
3438
|
+
if ( ref === void 0 ) ref = {};
|
|
3439
|
+
var insertAt = ref.insertAt;
|
|
3440
|
+
|
|
3441
|
+
if (!css || typeof document === 'undefined') { return; }
|
|
3442
|
+
|
|
3443
|
+
var head = document.head || document.getElementsByTagName('head')[0];
|
|
3444
|
+
var style = document.createElement('style');
|
|
3445
|
+
style.type = 'text/css';
|
|
3446
|
+
|
|
3447
|
+
if (insertAt === 'top') {
|
|
3448
|
+
if (head.firstChild) {
|
|
3449
|
+
head.insertBefore(style, head.firstChild);
|
|
3450
|
+
} else {
|
|
3451
|
+
head.appendChild(style);
|
|
3452
|
+
}
|
|
3453
|
+
} else {
|
|
3454
|
+
head.appendChild(style);
|
|
3455
|
+
}
|
|
3456
|
+
|
|
3457
|
+
if (style.styleSheet) {
|
|
3458
|
+
style.styleSheet.cssText = css;
|
|
3459
|
+
} else {
|
|
3460
|
+
style.appendChild(document.createTextNode(css));
|
|
3461
|
+
}
|
|
3462
|
+
}
|
|
3463
|
+
|
|
3464
|
+
var css_248z$1 = ".modal-overlay {\r\n position: fixed;\r\n top: 0;\r\n left: 0;\r\n right: 0;\r\n bottom: 0;\r\n background: rgba(0, 0, 0, 0.5);\r\n display: flex;\r\n align-items: center;\r\n justify-content: center;\r\n z-index: 1000;\r\n padding: 20px;\r\n}\r\n\r\n.modal-content {\r\n background: #ffffff;\r\n border-radius: 8px;\r\n box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);\r\n width: 100%;\r\n max-width: 500px;\r\n max-height: 90vh;\r\n overflow-y: auto;\r\n}\r\n\r\n.modal-header {\r\n display: flex;\r\n justify-content: space-between;\r\n align-items: center;\r\n padding: 20px;\r\n border-bottom: 1px solid #e9ecef;\r\n}\r\n\r\n.modal-header h3 {\r\n margin: 0;\r\n font-size: 20px;\r\n font-weight: 600;\r\n color: #212529;\r\n}\r\n\r\n.modal-close-btn {\r\n background: none;\r\n border: none;\r\n font-size: 32px;\r\n line-height: 1;\r\n color: #6c757d;\r\n cursor: pointer;\r\n padding: 0;\r\n width: 32px;\r\n height: 32px;\r\n display: flex;\r\n align-items: center;\r\n justify-content: center;\r\n border-radius: 4px;\r\n transition: background-color 0.2s;\r\n}\r\n\r\n.modal-close-btn:hover {\r\n background: #e9ecef;\r\n color: #212529;\r\n}\r\n\r\n.modal-body {\r\n padding: 20px;\r\n}\r\n\r\n.modal-footer {\r\n display: flex;\r\n justify-content: flex-end;\r\n gap: 12px;\r\n padding: 20px;\r\n border-top: 1px solid #e9ecef;\r\n}\r\n\r\n.modal-error {\r\n background: #f8d7da;\r\n color: #842029;\r\n padding: 12px;\r\n border-radius: 4px;\r\n margin-bottom: 16px;\r\n font-size: 14px;\r\n}\r\n\r\n.form-group {\r\n margin-bottom: 16px;\r\n}\r\n\r\n.form-group label {\r\n display: block;\r\n font-weight: 500;\r\n font-size: 14px;\r\n color: #212529;\r\n margin-bottom: 6px;\r\n}\r\n\r\n.form-group input,\r\n.form-group textarea {\r\n width: 100%;\r\n padding: 10px 12px;\r\n border: 1px solid #ced4da;\r\n border-radius: 4px;\r\n font-size: 14px;\r\n font-family: inherit;\r\n color: #212529;\r\n transition: border-color 0.2s;\r\n box-sizing: border-box;\r\n}\r\n\r\n.form-group input:focus,\r\n.form-group textarea:focus {\r\n outline: none;\r\n border-color: #0d6efd;\r\n box-shadow: 0 0 0 3px rgba(13, 110, 253, 0.1);\r\n}\r\n\r\n.form-group textarea {\r\n resize: vertical;\r\n}\r\n\r\n.form-group small {\r\n display: block;\r\n margin-top: 4px;\r\n font-size: 12px;\r\n color: #6c757d;\r\n}\r\n\r\n.form-row {\r\n display: grid;\r\n grid-template-columns: 1fr 1fr;\r\n gap: 12px;\r\n}\r\n\r\n.modal-btn {\r\n padding: 10px 20px;\r\n border: none;\r\n border-radius: 4px;\r\n font-size: 14px;\r\n font-weight: 500;\r\n cursor: pointer;\r\n transition: all 0.2s;\r\n}\r\n\r\n.modal-btn:disabled {\r\n opacity: 0.6;\r\n cursor: not-allowed;\r\n}\r\n\r\n.modal-btn-primary {\r\n background: #0d6efd;\r\n color: #ffffff;\r\n}\r\n\r\n.modal-btn-primary:hover:not(:disabled) {\r\n background: #0b5ed7;\r\n}\r\n\r\n.modal-btn-secondary {\r\n background: #6c757d;\r\n color: #ffffff;\r\n}\r\n\r\n.modal-btn-secondary:hover:not(:disabled) {\r\n background: #5c636a;\r\n}\r\n\r\n@media (max-width: 576px) {\r\n .modal-content {\r\n max-width: 100%;\r\n }\r\n\r\n .form-row {\r\n grid-template-columns: 1fr;\r\n }\r\n}\r\n";
|
|
3465
|
+
styleInject(css_248z$1);
|
|
3466
|
+
|
|
3467
|
+
const CreateBookingModal = ({ businessId, apiBaseUrl, initialDate, participants: defaultParticipants, onClose, onBookingCreated, }) => {
|
|
3468
|
+
const [title, setTitle] = React.useState('');
|
|
3469
|
+
const [description, setDescription] = React.useState('');
|
|
3470
|
+
const [startDate, setStartDate] = React.useState(format(initialDate, "yyyy-MM-dd"));
|
|
3471
|
+
const [startTime, setStartTime] = React.useState('09:00');
|
|
3472
|
+
const [endTime, setEndTime] = React.useState('10:00');
|
|
3473
|
+
const [participants, setParticipants] = React.useState(defaultParticipants.join(', '));
|
|
3474
|
+
const [loading, setLoading] = React.useState(false);
|
|
3475
|
+
const [error, setError] = React.useState(null);
|
|
3476
|
+
const handleSubmit = (e) => __awaiter(void 0, void 0, void 0, function* () {
|
|
3477
|
+
e.preventDefault();
|
|
3478
|
+
setLoading(true);
|
|
3479
|
+
setError(null);
|
|
3480
|
+
try {
|
|
3481
|
+
const participantList = participants
|
|
3482
|
+
.split(',')
|
|
3483
|
+
.map((p) => p.trim())
|
|
3484
|
+
.filter((p) => p.length > 0);
|
|
3485
|
+
if (participantList.length === 0) {
|
|
3486
|
+
throw new Error('At least one participant is required');
|
|
3487
|
+
}
|
|
3488
|
+
const startDateTime = new Date(`${startDate}T${startTime}:00.000Z`);
|
|
3489
|
+
const endDateTime = new Date(`${startDate}T${endTime}:00.000Z`);
|
|
3490
|
+
if (endDateTime <= startDateTime) {
|
|
3491
|
+
throw new Error('End time must be after start time');
|
|
3492
|
+
}
|
|
3493
|
+
const booking = yield createBooking(businessId, {
|
|
3494
|
+
participants: participantList,
|
|
3495
|
+
start: startDateTime.toISOString(),
|
|
3496
|
+
end: endDateTime.toISOString(),
|
|
3497
|
+
metadata: {
|
|
3498
|
+
title: title || 'Untitled Meeting',
|
|
3499
|
+
description,
|
|
3500
|
+
},
|
|
3501
|
+
}, apiBaseUrl);
|
|
3502
|
+
onBookingCreated(booking);
|
|
3503
|
+
}
|
|
3504
|
+
catch (err) {
|
|
3505
|
+
setError(err instanceof Error ? err.message : 'Failed to create booking');
|
|
3506
|
+
}
|
|
3507
|
+
finally {
|
|
3508
|
+
setLoading(false);
|
|
3509
|
+
}
|
|
3510
|
+
});
|
|
3511
|
+
return (React.createElement("div", { className: "modal-overlay", onClick: onClose },
|
|
3512
|
+
React.createElement("div", { className: "modal-content", onClick: (e) => e.stopPropagation() },
|
|
3513
|
+
React.createElement("div", { className: "modal-header" },
|
|
3514
|
+
React.createElement("h3", null, "Create New Booking"),
|
|
3515
|
+
React.createElement("button", { className: "modal-close-btn", onClick: onClose }, "\u00D7")),
|
|
3516
|
+
React.createElement("form", { onSubmit: handleSubmit },
|
|
3517
|
+
React.createElement("div", { className: "modal-body" },
|
|
3518
|
+
error && React.createElement("div", { className: "modal-error" }, error),
|
|
3519
|
+
React.createElement("div", { className: "form-group" },
|
|
3520
|
+
React.createElement("label", { htmlFor: "title" }, "Title"),
|
|
3521
|
+
React.createElement("input", { id: "title", type: "text", value: title, onChange: (e) => setTitle(e.target.value), placeholder: "Meeting title", required: true })),
|
|
3522
|
+
React.createElement("div", { className: "form-group" },
|
|
3523
|
+
React.createElement("label", { htmlFor: "description" }, "Description"),
|
|
3524
|
+
React.createElement("textarea", { id: "description", value: description, onChange: (e) => setDescription(e.target.value), placeholder: "Meeting description (optional)", rows: 3 })),
|
|
3525
|
+
React.createElement("div", { className: "form-group" },
|
|
3526
|
+
React.createElement("label", { htmlFor: "participants" }, "Participants"),
|
|
3527
|
+
React.createElement("input", { id: "participants", type: "text", value: participants, onChange: (e) => setParticipants(e.target.value), placeholder: "user_A, room_101 (comma-separated)", required: true }),
|
|
3528
|
+
React.createElement("small", null, "Enter participant IDs separated by commas")),
|
|
3529
|
+
React.createElement("div", { className: "form-row" },
|
|
3530
|
+
React.createElement("div", { className: "form-group" },
|
|
3531
|
+
React.createElement("label", { htmlFor: "date" }, "Date"),
|
|
3532
|
+
React.createElement("input", { id: "date", type: "date", value: startDate, onChange: (e) => setStartDate(e.target.value), required: true }))),
|
|
3533
|
+
React.createElement("div", { className: "form-row" },
|
|
3534
|
+
React.createElement("div", { className: "form-group" },
|
|
3535
|
+
React.createElement("label", { htmlFor: "startTime" }, "Start Time"),
|
|
3536
|
+
React.createElement("input", { id: "startTime", type: "time", value: startTime, onChange: (e) => setStartTime(e.target.value), required: true })),
|
|
3537
|
+
React.createElement("div", { className: "form-group" },
|
|
3538
|
+
React.createElement("label", { htmlFor: "endTime" }, "End Time"),
|
|
3539
|
+
React.createElement("input", { id: "endTime", type: "time", value: endTime, onChange: (e) => setEndTime(e.target.value), required: true })))),
|
|
3540
|
+
React.createElement("div", { className: "modal-footer" },
|
|
3541
|
+
React.createElement("button", { type: "button", className: "modal-btn modal-btn-secondary", onClick: onClose, disabled: loading }, "Cancel"),
|
|
3542
|
+
React.createElement("button", { type: "submit", className: "modal-btn modal-btn-primary", disabled: loading }, loading ? 'Creating...' : 'Create Booking'))))));
|
|
3543
|
+
};
|
|
3544
|
+
|
|
3545
|
+
var css_248z = ".calendar-container {\r\n width: 100%;\r\n font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;\r\n background: #ffffff;\r\n border-radius: 8px;\r\n box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);\r\n overflow: hidden;\r\n}\r\n\r\n.calendar-header {\r\n background: #f8f9fa;\r\n padding: 20px;\r\n border-bottom: 1px solid #e9ecef;\r\n}\r\n\r\n.calendar-title-section {\r\n display: flex;\r\n justify-content: space-between;\r\n align-items: center;\r\n margin-bottom: 16px;\r\n}\r\n\r\n.calendar-title {\r\n margin: 0;\r\n font-size: 24px;\r\n font-weight: 600;\r\n color: #212529;\r\n}\r\n\r\n.calendar-navigation {\r\n display: flex;\r\n align-items: center;\r\n gap: 12px;\r\n}\r\n\r\n.calendar-current-month {\r\n font-size: 18px;\r\n font-weight: 500;\r\n color: #495057;\r\n margin-left: 12px;\r\n}\r\n\r\n.calendar-btn {\r\n padding: 8px 16px;\r\n border: 1px solid #dee2e6;\r\n background: #ffffff;\r\n border-radius: 4px;\r\n cursor: pointer;\r\n font-size: 14px;\r\n font-weight: 500;\r\n color: #495057;\r\n transition: all 0.2s;\r\n}\r\n\r\n.calendar-btn:hover {\r\n background: #e9ecef;\r\n border-color: #adb5bd;\r\n}\r\n\r\n.calendar-btn-create {\r\n background: #0d6efd;\r\n color: #ffffff;\r\n border-color: #0d6efd;\r\n}\r\n\r\n.calendar-btn-create:hover {\r\n background: #0b5ed7;\r\n border-color: #0a58ca;\r\n}\r\n\r\n.calendar-days-row {\r\n display: grid;\r\n grid-template-columns: repeat(7, 1fr);\r\n background: #f8f9fa;\r\n border-bottom: 2px solid #dee2e6;\r\n}\r\n\r\n.calendar-day-header {\r\n padding: 12px;\r\n text-align: center;\r\n font-weight: 600;\r\n font-size: 14px;\r\n color: #6c757d;\r\n text-transform: uppercase;\r\n}\r\n\r\n.calendar-body {\r\n background: #ffffff;\r\n}\r\n\r\n.calendar-row {\r\n display: grid;\r\n grid-template-columns: repeat(7, 1fr);\r\n border-bottom: 1px solid #e9ecef;\r\n}\r\n\r\n.calendar-row:last-child {\r\n border-bottom: none;\r\n}\r\n\r\n.calendar-cell {\r\n min-height: 120px;\r\n padding: 8px;\r\n border-right: 1px solid #e9ecef;\r\n cursor: pointer;\r\n transition: background-color 0.2s;\r\n position: relative;\r\n}\r\n\r\n.calendar-cell:last-child {\r\n border-right: none;\r\n}\r\n\r\n.calendar-cell:hover {\r\n background: #f8f9fa;\r\n}\r\n\r\n.calendar-cell-disabled {\r\n background: #f8f9fa;\r\n opacity: 0.5;\r\n cursor: not-allowed;\r\n}\r\n\r\n.calendar-cell-disabled:hover {\r\n background: #f8f9fa;\r\n}\r\n\r\n.calendar-cell-today {\r\n background: #e7f5ff;\r\n}\r\n\r\n.calendar-cell-today .calendar-cell-number {\r\n background: #0d6efd;\r\n color: #ffffff;\r\n}\r\n\r\n.calendar-cell-number {\r\n display: inline-flex;\r\n align-items: center;\r\n justify-content: center;\r\n width: 28px;\r\n height: 28px;\r\n border-radius: 50%;\r\n font-weight: 600;\r\n font-size: 14px;\r\n color: #212529;\r\n margin-bottom: 4px;\r\n}\r\n\r\n.calendar-cell-bookings {\r\n margin-top: 4px;\r\n}\r\n\r\n.calendar-booking {\r\n background: #0d6efd;\r\n color: #ffffff;\r\n padding: 4px 8px;\r\n margin-bottom: 4px;\r\n border-radius: 4px;\r\n font-size: 12px;\r\n cursor: pointer;\r\n transition: background-color 0.2s;\r\n display: flex;\r\n gap: 4px;\r\n overflow: hidden;\r\n text-overflow: ellipsis;\r\n white-space: nowrap;\r\n}\r\n\r\n.calendar-booking:hover {\r\n background: #0b5ed7;\r\n}\r\n\r\n.calendar-booking-time {\r\n font-weight: 600;\r\n}\r\n\r\n.calendar-booking-title {\r\n flex: 1;\r\n overflow: hidden;\r\n text-overflow: ellipsis;\r\n}\r\n\r\n.calendar-booking-more {\r\n color: #6c757d;\r\n font-size: 11px;\r\n padding: 2px 4px;\r\n font-weight: 500;\r\n}\r\n\r\n.calendar-error {\r\n background: #f8d7da;\r\n color: #842029;\r\n padding: 12px 20px;\r\n border-bottom: 1px solid #f5c2c7;\r\n}\r\n\r\n.calendar-loading {\r\n background: #cff4fc;\r\n color: #055160;\r\n padding: 12px 20px;\r\n border-bottom: 1px solid #b6effb;\r\n text-align: center;\r\n}\r\n\r\n@media (max-width: 768px) {\r\n .calendar-title-section {\r\n flex-direction: column;\r\n align-items: flex-start;\r\n gap: 12px;\r\n }\r\n\r\n .calendar-cell {\r\n min-height: 80px;\r\n padding: 4px;\r\n }\r\n\r\n .calendar-cell-number {\r\n width: 24px;\r\n height: 24px;\r\n font-size: 12px;\r\n }\r\n\r\n .calendar-booking {\r\n font-size: 10px;\r\n padding: 2px 4px;\r\n }\r\n}\r\n";
|
|
3546
|
+
styleInject(css_248z);
|
|
3547
|
+
|
|
3548
|
+
const Calendar = ({ businessId, resourceId, title = 'Calendar', apiBaseUrl, defaultView = 'month', onBookingCreate, onBookingClick, participants = [], }) => {
|
|
3549
|
+
const [currentDate, setCurrentDate] = React.useState(new Date());
|
|
3550
|
+
const [bookings, setBookings] = React.useState([]);
|
|
3551
|
+
const [loading, setLoading] = React.useState(false);
|
|
3552
|
+
const [error, setError] = React.useState(null);
|
|
3553
|
+
const [showCreateModal, setShowCreateModal] = React.useState(false);
|
|
3554
|
+
const [selectedDate, setSelectedDate] = React.useState(null);
|
|
3555
|
+
const fetchBookings = React.useCallback(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
3556
|
+
setLoading(true);
|
|
3557
|
+
setError(null);
|
|
3558
|
+
try {
|
|
3559
|
+
const monthStart = startOfMonth(currentDate);
|
|
3560
|
+
const monthEnd = endOfMonth(currentDate);
|
|
3561
|
+
const startDate = startOfWeek(monthStart);
|
|
3562
|
+
const endDate = endOfWeek(monthEnd);
|
|
3563
|
+
let fetchedBookings;
|
|
3564
|
+
if (resourceId) {
|
|
3565
|
+
fetchedBookings = yield getResourceSchedule(businessId, resourceId, startDate.toISOString(), endDate.toISOString(), apiBaseUrl);
|
|
3566
|
+
}
|
|
3567
|
+
else {
|
|
3568
|
+
fetchedBookings = yield getAllBookings(businessId, startDate.toISOString(), endDate.toISOString(), apiBaseUrl);
|
|
3569
|
+
}
|
|
3570
|
+
setBookings(fetchedBookings);
|
|
3571
|
+
}
|
|
3572
|
+
catch (err) {
|
|
3573
|
+
setError(err instanceof Error ? err.message : 'Failed to load bookings');
|
|
3574
|
+
}
|
|
3575
|
+
finally {
|
|
3576
|
+
setLoading(false);
|
|
3577
|
+
}
|
|
3578
|
+
}), [businessId, resourceId, currentDate, apiBaseUrl]);
|
|
3579
|
+
React.useEffect(() => {
|
|
3580
|
+
fetchBookings();
|
|
3581
|
+
}, [fetchBookings]);
|
|
3582
|
+
const handlePreviousMonth = () => {
|
|
3583
|
+
setCurrentDate(subMonths(currentDate, 1));
|
|
3584
|
+
};
|
|
3585
|
+
const handleNextMonth = () => {
|
|
3586
|
+
setCurrentDate(addMonths(currentDate, 1));
|
|
3587
|
+
};
|
|
3588
|
+
const handleToday = () => {
|
|
3589
|
+
setCurrentDate(new Date());
|
|
3590
|
+
};
|
|
3591
|
+
const handleDateClick = (day) => {
|
|
3592
|
+
setSelectedDate(day);
|
|
3593
|
+
setShowCreateModal(true);
|
|
3594
|
+
};
|
|
3595
|
+
const handleBookingCreated = (booking) => __awaiter(void 0, void 0, void 0, function* () {
|
|
3596
|
+
setShowCreateModal(false);
|
|
3597
|
+
setSelectedDate(null);
|
|
3598
|
+
yield fetchBookings();
|
|
3599
|
+
if (onBookingCreate) {
|
|
3600
|
+
onBookingCreate(booking);
|
|
3601
|
+
}
|
|
3602
|
+
});
|
|
3603
|
+
const getBookingsForDay = (day) => {
|
|
3604
|
+
return bookings.filter((booking) => {
|
|
3605
|
+
const bookingStart = parseISO(booking.start);
|
|
3606
|
+
return isSameDay(bookingStart, day);
|
|
3607
|
+
});
|
|
3608
|
+
};
|
|
3609
|
+
const renderHeader = () => {
|
|
3610
|
+
return (React.createElement("div", { className: "calendar-header" },
|
|
3611
|
+
React.createElement("div", { className: "calendar-title-section" },
|
|
3612
|
+
React.createElement("h2", { className: "calendar-title" }, title),
|
|
3613
|
+
React.createElement("button", { className: "calendar-btn calendar-btn-create", onClick: () => setShowCreateModal(true) }, "+ Create Booking")),
|
|
3614
|
+
React.createElement("div", { className: "calendar-navigation" },
|
|
3615
|
+
React.createElement("button", { className: "calendar-btn", onClick: handlePreviousMonth }, "\u2039"),
|
|
3616
|
+
React.createElement("button", { className: "calendar-btn", onClick: handleToday }, "Today"),
|
|
3617
|
+
React.createElement("button", { className: "calendar-btn", onClick: handleNextMonth }, "\u203A"),
|
|
3618
|
+
React.createElement("span", { className: "calendar-current-month" }, format(currentDate, 'MMMM yyyy')))));
|
|
3619
|
+
};
|
|
3620
|
+
const renderDaysOfWeek = () => {
|
|
3621
|
+
const days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
|
|
3622
|
+
return (React.createElement("div", { className: "calendar-days-row" }, days.map((day) => (React.createElement("div", { key: day, className: "calendar-day-header" }, day)))));
|
|
3623
|
+
};
|
|
3624
|
+
const renderCells = () => {
|
|
3625
|
+
const monthStart = startOfMonth(currentDate);
|
|
3626
|
+
const monthEnd = endOfMonth(currentDate);
|
|
3627
|
+
const startDate = startOfWeek(monthStart);
|
|
3628
|
+
const endDate = endOfWeek(monthEnd);
|
|
3629
|
+
const rows = [];
|
|
3630
|
+
let days = [];
|
|
3631
|
+
let day = startDate;
|
|
3632
|
+
while (day <= endDate) {
|
|
3633
|
+
for (let i = 0; i < 7; i++) {
|
|
3634
|
+
const currentDay = day;
|
|
3635
|
+
const dayBookings = getBookingsForDay(currentDay);
|
|
3636
|
+
const isCurrentMonth = isSameMonth(currentDay, monthStart);
|
|
3637
|
+
const isToday = isSameDay(currentDay, new Date());
|
|
3638
|
+
days.push(React.createElement("div", { key: day.toString(), className: `calendar-cell ${!isCurrentMonth ? 'calendar-cell-disabled' : ''} ${isToday ? 'calendar-cell-today' : ''}`, onClick: () => isCurrentMonth && handleDateClick(currentDay) },
|
|
3639
|
+
React.createElement("div", { className: "calendar-cell-number" }, format(currentDay, 'd')),
|
|
3640
|
+
React.createElement("div", { className: "calendar-cell-bookings" },
|
|
3641
|
+
dayBookings.slice(0, 3).map((booking, idx) => {
|
|
3642
|
+
var _a, _b;
|
|
3643
|
+
return (React.createElement("div", { key: booking.meeting_id, className: "calendar-booking", onClick: (e) => {
|
|
3644
|
+
e.stopPropagation();
|
|
3645
|
+
if (onBookingClick) {
|
|
3646
|
+
onBookingClick(booking);
|
|
3647
|
+
}
|
|
3648
|
+
}, title: ((_a = booking.metadata) === null || _a === void 0 ? void 0 : _a.title) || 'Untitled' },
|
|
3649
|
+
React.createElement("span", { className: "calendar-booking-time" }, format(parseISO(booking.start), 'HH:mm')),
|
|
3650
|
+
React.createElement("span", { className: "calendar-booking-title" }, ((_b = booking.metadata) === null || _b === void 0 ? void 0 : _b.title) || 'Untitled')));
|
|
3651
|
+
}),
|
|
3652
|
+
dayBookings.length > 3 && (React.createElement("div", { className: "calendar-booking-more" },
|
|
3653
|
+
"+",
|
|
3654
|
+
dayBookings.length - 3,
|
|
3655
|
+
" more")))));
|
|
3656
|
+
day = addDays(day, 1);
|
|
3657
|
+
}
|
|
3658
|
+
rows.push(React.createElement("div", { key: day.toString(), className: "calendar-row" }, days));
|
|
3659
|
+
days = [];
|
|
3660
|
+
}
|
|
3661
|
+
return React.createElement("div", { className: "calendar-body" }, rows);
|
|
3662
|
+
};
|
|
3663
|
+
return (React.createElement("div", { className: "calendar-container" },
|
|
3664
|
+
renderHeader(),
|
|
3665
|
+
error && React.createElement("div", { className: "calendar-error" }, error),
|
|
3666
|
+
loading && React.createElement("div", { className: "calendar-loading" }, "Loading..."),
|
|
3667
|
+
renderDaysOfWeek(),
|
|
3668
|
+
renderCells(),
|
|
3669
|
+
showCreateModal && (React.createElement(CreateBookingModal, { businessId: businessId, apiBaseUrl: apiBaseUrl, initialDate: selectedDate || new Date(), participants: participants, onClose: () => {
|
|
3670
|
+
setShowCreateModal(false);
|
|
3671
|
+
setSelectedDate(null);
|
|
3672
|
+
}, onBookingCreated: handleBookingCreated }))));
|
|
3673
|
+
};
|
|
3674
|
+
|
|
3675
|
+
exports.Calendar = Calendar;
|
|
3676
|
+
exports.createBooking = createBooking;
|
|
3677
|
+
exports.getAllBookings = getAllBookings;
|
|
3678
|
+
exports.getMeetingDetails = getMeetingDetails;
|
|
3679
|
+
exports.getResourceSchedule = getResourceSchedule;
|
|
3680
|
+
//# sourceMappingURL=index.js.map
|