1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643 |
x134
x134
x134
x5
x5
x5
x131
x131
x131
x131
x708
x708
x708
x708
x2992
x748
x748
x708
x2852
x713
x713
x708
x3048
x762
x762
x708
x2848
x712
x712
x708
x3052
x763
x763
x708
x2848
x712
x712
x708
x2984
x746
x746
x708
x2844
x711
x711
x708
x3600
x720
x720
x708
x3560
x712
x712
x708
x3008
x752
x752
x708
x2844
x711
x711
x708
x2956
x739
x739
x708
x2848
x712
x712
x708
x2876
x719
x719
x708
x2844
x711
x711
x708
x2844
x711
x711
x708
x2940
x735
x735
x708
x940
x708
x709
x2836
x709
x709
x709
x939
x939
x3756
x939
x939
x131
x131
x66
x66
x66
x66
x66
x66
x66
x66
x66
x66
x66
x66
x66
x493
x493
x662
x662
x493
x66
x66
x66
x5
x5
x5
x131
x131
x5
x79
x79
x79
x356
x356
x726
x370
x370
x751
x381
x381
x381
x743
x373
x373
x373
x370
x370
x370
x726
x370
x370
x743
x373
x373
x373
x751
x381
x381
x381
x370
x370
x370
x726
x370
x370
x743
x373
x373
x373
x751
x381
x381
x381
x370
x370
x370
x750
x394
x394
x410
x421
x410
x394
x795
x401
x401
x401
x819
x425
x425
x425
x394
x394
x394
x745
x389
x389
x782
x393
x393
x393
x807
x418
x418
x418
x389
x389
x389
x744
x388
x388
x780
x392
x392
x392
x804
x416
x416
x416
x388
x388
x388
x728
x372
x372
x372
x372
x372
x372
x725
x369
x369
x369
x815
x459
x459
x459
x356
x356
x79
x79
x5
x92
x92
x371
x371
x371
x371
x778
x407
x843
x436
x436
x436
x436
x821
x414
x414
x414
x414
x407
x407
x407
x781
x410
x822
x412
x412
x412
x412
x857
x447
x447
x447
x447
x410
x410
x410
x784
x413
x828
x415
x415
x415
x415
x866
x453
x453
x453
x453
x413
x413
x413
x762
x391
x785
x394
x394
x394
x395
x395
x395
x394
x394
x799
x408
x408
x408
x409
x409
x409
x408
x408
x391
x391
x391
x758
x387
x775
x388
x388
x388
x388
x789
x402
x402
x402
x402
x387
x387
x387
x747
x376
x754
x378
x378
x378
x378
x755
x379
x379
x379
x379
x376
x376
x376
x757
x386
x386
x386
x386
x386
x751
x380
x380
x380
x381
x381
x381
x380
x381
x381
x381
x380
x381
x381
x381
x380
x382
x382
x382
x380
x382
x382
x382
x380
x382
x382
x382
x380
x380
x380
x839
x468
x470
x470
x470
x563
x468
x468
x468
x371
x371
x378
x378
x378
x378
x378
x378
x378
x2564
x641
x641
x92
x99
x99
x99
x163
x92
x5
x66
x66
x66
x66
x66
x66
x66
x363
x753
x390
x390
x390
x390
x766
x403
x403
x441
x441
x441
x403
x405
x403
x403
x767
x404
x404
x404
x404
x760
x397
x397
x397
x397
x420
x420
x420
x420
x429
x420
x420
x420
x429
x429
x420
x425
x425
x420
x420
x397
x397
x397
x743
x380
x380
x380
x380
x731
x368
x368
x368
x368
x731
x368
x368
x368
x368
x363
x363
x122
x66
x5
x44
x44
x44
x5 |
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
|
// Copyright 2018-2025 the Deno authors. MIT license.
// This module is browser compatible.
function digits(value: string | number, count = 2): string {
return String(value).padStart(count, "0");
}
// as declared as in namespace Intl
type DateTimeFormatPartTypes =
| "day"
| "dayPeriod"
// | "era"
| "hour"
| "literal"
| "minute"
| "month"
| "second"
| "timeZoneName"
// | "weekday"
| "year"
| "fractionalSecond";
interface DateTimeFormatPart {
type: DateTimeFormatPartTypes;
value: string;
}
type TimeZone = "UTC";
interface Options {
timeZone?: TimeZone;
}
type FormatPart = {
type: DateTimeFormatPartTypes;
value: string | number;
hour12?: boolean;
};
const QUOTED_LITERAL_REGEXP = /^(')(?<value>\\.|[^\']*)\1/;
const LITERAL_REGEXP = /^(?<value>.+?\s*)/;
const SYMBOL_REGEXP = /^(?<symbol>([a-zA-Z])\2*)/;
// according to unicode symbols (http://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table)
function formatToFormatParts(format: string) {
const formatParts: FormatPart[] = [];
let index = 0;
while (index < format.length) {
const substring = format.slice(index);
const symbol = SYMBOL_REGEXP.exec(substring)?.groups?.symbol;
switch (symbol) {
case "yyyy":
formatParts.push({ type: "year", value: "numeric" });
index += symbol.length;
continue;
case "yy":
formatParts.push({ type: "year", value: "2-digit" });
index += symbol.length;
continue;
case "MM":
formatParts.push({ type: "month", value: "2-digit" });
index += symbol.length;
continue;
case "M":
formatParts.push({ type: "month", value: "numeric" });
index += symbol.length;
continue;
case "dd":
formatParts.push({ type: "day", value: "2-digit" });
index += symbol.length;
continue;
case "d":
formatParts.push({ type: "day", value: "numeric" });
index += symbol.length;
continue;
case "HH":
formatParts.push({ type: "hour", value: "2-digit" });
index += symbol.length;
continue;
case "H":
formatParts.push({ type: "hour", value: "numeric" });
index += symbol.length;
continue;
case "hh":
formatParts.push({ type: "hour", value: "2-digit", hour12: true });
index += symbol.length;
continue;
case "h":
formatParts.push({ type: "hour", value: "numeric", hour12: true });
index += symbol.length;
continue;
case "mm":
formatParts.push({ type: "minute", value: "2-digit" });
index += symbol.length;
continue;
case "m":
formatParts.push({ type: "minute", value: "numeric" });
index += symbol.length;
continue;
case "ss":
formatParts.push({ type: "second", value: "2-digit" });
index += symbol.length;
continue;
case "s":
formatParts.push({ type: "second", value: "numeric" });
index += symbol.length;
continue;
case "SSS":
formatParts.push({ type: "fractionalSecond", value: 3 });
index += symbol.length;
continue;
case "SS":
formatParts.push({ type: "fractionalSecond", value: 2 });
index += symbol.length;
continue;
case "S":
formatParts.push({ type: "fractionalSecond", value: 1 });
index += symbol.length;
continue;
case "a":
formatParts.push({ type: "dayPeriod", value: 1 });
index += symbol.length;
continue;
}
const quotedLiteralMatch = QUOTED_LITERAL_REGEXP.exec(substring);
if (quotedLiteralMatch) {
const value = quotedLiteralMatch.groups!.value as string;
formatParts.push({ type: "literal", value });
index += quotedLiteralMatch[0].length;
continue;
}
const literalGroups = LITERAL_REGEXP.exec(substring)!.groups!;
const value = literalGroups.value as string;
formatParts.push({ type: "literal", value });
index += value.length;
}
return formatParts;
}
function sortDateTimeFormatParts(
parts: DateTimeFormatPart[],
): DateTimeFormatPart[] {
let result: DateTimeFormatPart[] = [];
const typeArray = [
"year",
"month",
"day",
"hour",
"minute",
"second",
"fractionalSecond",
];
for (const type of typeArray) {
const current = parts.findIndex((el) => el.type === type);
if (current !== -1) {
result = result.concat(parts.splice(current, 1));
}
}
result = result.concat(parts);
return result;
}
export class DateTimeFormatter {
#formatParts: FormatPart[];
constructor(formatString: string) {
this.#formatParts = formatToFormatParts(formatString);
}
format(date: Date, options: Options = {}): string {
let string = "";
const utc = options.timeZone === "UTC";
for (const part of this.#formatParts) {
const type = part.type;
switch (type) {
case "year": {
const value = utc ? date.getUTCFullYear() : date.getFullYear();
switch (part.value) {
case "numeric": {
string += value;
break;
}
case "2-digit": {
string += digits(value, 2).slice(-2);
break;
}
default:
throw new Error(
`FormatterError: value "${part.value}" is not supported`,
);
}
break;
}
case "month": {
const value = (utc ? date.getUTCMonth() : date.getMonth()) + 1;
switch (part.value) {
case "numeric": {
string += value;
break;
}
case "2-digit": {
string += digits(value, 2);
break;
}
default:
throw new Error(
`FormatterError: value "${part.value}" is not supported`,
);
}
break;
}
case "day": {
const value = utc ? date.getUTCDate() : date.getDate();
switch (part.value) {
case "numeric": {
string += value;
break;
}
case "2-digit": {
string += digits(value, 2);
break;
}
default:
throw new Error(
`FormatterError: value "${part.value}" is not supported`,
);
}
break;
}
case "hour": {
let value = utc ? date.getUTCHours() : date.getHours();
if (part.hour12) {
if (value === 0) value = 12;
else if (value > 12) value -= 12;
}
switch (part.value) {
case "numeric": {
string += value;
break;
}
case "2-digit": {
string += digits(value, 2);
break;
}
default:
throw new Error(
`FormatterError: value "${part.value}" is not supported`,
);
}
break;
}
case "minute": {
const value = utc ? date.getUTCMinutes() : date.getMinutes();
switch (part.value) {
case "numeric": {
string += value;
break;
}
case "2-digit": {
string += digits(value, 2);
break;
}
default:
throw new Error(
`FormatterError: value "${part.value}" is not supported`,
);
}
break;
}
case "second": {
const value = utc ? date.getUTCSeconds() : date.getSeconds();
switch (part.value) {
case "numeric": {
string += value;
break;
}
case "2-digit": {
string += digits(value, 2);
break;
}
default:
throw new Error(
`FormatterError: value "${part.value}" is not supported`,
);
}
break;
}
case "fractionalSecond": {
const value = utc
? date.getUTCMilliseconds()
: date.getMilliseconds();
string += digits(value, 3).slice(0, Number(part.value));
break;
}
// FIXME(bartlomieju)
case "timeZoneName": {
// string += utc ? "Z" : part.value
break;
}
case "dayPeriod": {
string += date.getHours() >= 12 ? "PM" : "AM";
break;
}
case "literal": {
string += part.value;
break;
}
default:
throw new Error(`FormatterError: { ${part.type} ${part.value} }`);
}
}
return string;
}
formatToParts(string: string): DateTimeFormatPart[] {
const parts: DateTimeFormatPart[] = [];
for (const part of this.#formatParts) {
const type = part.type;
let length = 0;
let value = "";
switch (part.type) {
case "year": {
switch (part.value) {
case "numeric": {
value = /^\d{4}/.exec(string)?.[0] as string;
length = value?.length;
break;
}
case "2-digit": {
value = /^\d{2}/.exec(string)?.[0] as string;
length = value?.length;
break;
}
default:
throw new Error(
`ParserError: value "${part.value}" is not supported`,
);
}
break;
}
case "month": {
switch (part.value) {
case "numeric": {
value = /^\d{1,2}/.exec(string)?.[0] as string;
length = value?.length;
break;
}
case "2-digit": {
value = /^\d{2}/.exec(string)?.[0] as string;
length = value?.length;
break;
}
case "narrow": {
value = /^[a-zA-Z]+/.exec(string)?.[0] as string;
length = value?.length;
break;
}
case "short": {
value = /^[a-zA-Z]+/.exec(string)?.[0] as string;
length = value?.length;
break;
}
case "long": {
value = /^[a-zA-Z]+/.exec(string)?.[0] as string;
length = value?.length;
break;
}
default:
throw new Error(
`ParserError: value "${part.value}" is not supported`,
);
}
break;
}
case "day": {
switch (part.value) {
case "numeric": {
value = /^\d{1,2}/.exec(string)?.[0] as string;
length = value?.length;
break;
}
case "2-digit": {
value = /^\d{2}/.exec(string)?.[0] as string;
length = value?.length;
break;
}
default:
throw new Error(
`ParserError: value "${part.value}" is not supported`,
);
}
break;
}
case "hour": {
switch (part.value) {
case "numeric": {
value = /^\d{1,2}/.exec(string)?.[0] as string;
length = value?.length;
if (part.hour12 && parseInt(value) > 12) {
// TODO(iuioiua): Replace with throwing an error
// deno-lint-ignore no-console
console.error(
`Trying to parse hour greater than 12, use 'H' instead of 'h'.`,
);
}
break;
}
case "2-digit": {
value = /^\d{2}/.exec(string)?.[0] as string;
length = value?.length;
if (part.hour12 && parseInt(value) > 12) {
// TODO(iuioiua): Replace with throwing an error
// deno-lint-ignore no-console
console.error(
`Trying to parse hour greater than 12, use 'HH' instead of 'hh'.`,
);
}
break;
}
default:
// TODO(iuioiua): Correct error type and message
throw new Error(
`ParserError: value "${part.value}" is not supported`,
);
}
break;
}
case "minute": {
switch (part.value) {
case "numeric": {
value = /^\d{1,2}/.exec(string)?.[0] as string;
length = value?.length;
break;
}
case "2-digit": {
value = /^\d{2}/.exec(string)?.[0] as string;
length = value?.length;
break;
}
default:
throw new Error(
`ParserError: value "${part.value}" is not supported`,
);
}
break;
}
case "second": {
switch (part.value) {
case "numeric": {
value = /^\d{1,2}/.exec(string)?.[0] as string;
length = value?.length;
break;
}
case "2-digit": {
value = /^\d{2}/.exec(string)?.[0] as string;
length = value?.length;
break;
}
default:
throw new Error(
`ParserError: value "${part.value}" is not supported`,
);
}
break;
}
case "fractionalSecond": {
value = new RegExp(`^\\d{${part.value}}`).exec(string)
?.[0] as string;
length = value?.length;
break;
}
case "timeZoneName": {
value = part.value as string;
length = value?.length;
break;
}
case "dayPeriod": {
value = /^[AP](?:\.M\.|M\.?)/i.exec(string)?.[0] as string;
switch (value.toUpperCase()) {
case "AM":
value = "AM";
length = 2;
break;
case "AM.":
value = "AM";
length = 3;
break;
case "A.M.":
value = "AM";
length = 4;
break;
case "PM":
value = "PM";
length = 2;
break;
case "PM.":
value = "PM";
length = 3;
break;
case "P.M.":
value = "PM";
length = 4;
break;
default:
throw new Error(`DayPeriod '${value}' is not supported.`);
}
break;
}
case "literal": {
if (!string.startsWith(part.value as string)) {
throw new Error(
`Literal "${part.value}" not found "${string.slice(0, 25)}"`,
);
}
value = part.value as string;
length = value?.length;
break;
}
default:
throw new Error(
`Cannot format the date, the value (${part.value}) of the type (${part.type}) is given`,
);
}
if (!value) {
throw new Error(
`Cannot format value: The value is not valid for part { ${type} ${value} } ${
string.slice(
0,
25,
)
}`,
);
}
parts.push({ type, value });
string = string.slice(length);
}
if (string.length) {
throw new Error(
`datetime string was not fully parsed! ${string.slice(0, 25)}`,
);
}
return parts;
}
partsToDate(parts: DateTimeFormatPart[]): Date {
parts = sortDateTimeFormatParts(parts);
const date = new Date();
const utc = parts.find(
(part) => part.type === "timeZoneName" && part.value === "UTC",
);
const dayPart = parts.find((part) => part.type === "day");
utc ? date.setUTCHours(0, 0, 0, 0) : date.setHours(0, 0, 0, 0);
for (const part of parts) {
switch (part.type) {
case "year": {
const value = Number(part.value.padStart(4, "20"));
utc ? date.setUTCFullYear(value) : date.setFullYear(value);
break;
}
case "month": {
const value = Number(part.value) - 1;
if (dayPart) {
utc
? date.setUTCMonth(value, Number(dayPart.value))
: date.setMonth(value, Number(dayPart.value));
} else {
utc ? date.setUTCMonth(value) : date.setMonth(value);
}
break;
}
case "day": {
const value = Number(part.value);
utc ? date.setUTCDate(value) : date.setDate(value);
break;
}
case "hour": {
let value = Number(part.value);
const dayPeriod = parts.find(
(part: DateTimeFormatPart) => part.type === "dayPeriod",
);
if (dayPeriod) {
switch (dayPeriod.value.toUpperCase()) {
case "AM":
case "AM.":
case "A.M.":
// ignore
break;
case "PM":
case "PM.":
case "P.M.":
value += 12;
break;
default:
throw new Error(
`dayPeriod '${dayPeriod.value}' is not supported.`,
);
}
}
utc ? date.setUTCHours(value) : date.setHours(value);
break;
}
case "minute": {
const value = Number(part.value);
utc ? date.setUTCMinutes(value) : date.setMinutes(value);
break;
}
case "second": {
const value = Number(part.value);
utc ? date.setUTCSeconds(value) : date.setSeconds(value);
break;
}
case "fractionalSecond": {
const value = Number(part.value);
utc ? date.setUTCMilliseconds(value) : date.setMilliseconds(value);
break;
}
}
}
return date;
}
parse(string: string): Date {
const parts = this.formatToParts(string);
return this.partsToDate(parts);
}
}
|