1 | import { toDate } from "./toDate.js";
|
2 |
|
3 | /**
|
4 | * The locale string (see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locales_argument).
|
5 | * @deprecated
|
6 | *
|
7 | * [TODO] Remove in v4
|
8 | */
|
9 |
|
10 | /**
|
11 | * The format options (see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#options)
|
12 | */
|
13 |
|
14 | /**
|
15 | * The locale options.
|
16 | */
|
17 |
|
18 | /**
|
19 | * @name intlFormat
|
20 | * @category Common Helpers
|
21 | * @summary Format the date with Intl.DateTimeFormat (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat).
|
22 | *
|
23 | * @description
|
24 | * Return the formatted date string in the given format.
|
25 | * The method uses [`Intl.DateTimeFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat) inside.
|
26 | * formatOptions are the same as [`Intl.DateTimeFormat` options](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat#using_options)
|
27 | *
|
28 | * > ⚠️ Please note that before Node version 13.0.0, only the locale data for en-US is available by default.
|
29 | *
|
30 | * @param date - The date to format
|
31 | *
|
32 | * @returns The formatted date string
|
33 | *
|
34 | * @throws `date` must not be Invalid Date
|
35 | *
|
36 | * @example
|
37 | * // Represent 4 October 2019 in middle-endian format:
|
38 | * const result = intlFormat(new Date(2019, 9, 4, 12, 30, 13, 456))
|
39 | * //=> 10/4/2019
|
40 | */
|
41 |
|
42 | /**
|
43 | * @param date - The date to format
|
44 | * @param localeOptions - An object with locale
|
45 | *
|
46 | * @returns The formatted date string
|
47 | *
|
48 | * @throws `date` must not be Invalid Date
|
49 | *
|
50 | * @example
|
51 | * // Represent 4 October 2019 in Korean.
|
52 | * // Convert the date with locale's options.
|
53 | * const result = intlFormat(new Date(2019, 9, 4, 12, 30, 13, 456), {
|
54 | * locale: 'ko-KR',
|
55 | * })
|
56 | * //=> 2019. 10. 4.
|
57 | */
|
58 |
|
59 | /**
|
60 | * @param date - The date to format
|
61 | * @param formatOptions - The format options
|
62 | *
|
63 | * @returns The formatted date string
|
64 | *
|
65 | * @throws `date` must not be Invalid Date
|
66 | *
|
67 | * @example
|
68 | * // Represent 4 October 2019.
|
69 | * // Convert the date with format's options.
|
70 | * const result = intlFormat.default(new Date(2019, 9, 4, 12, 30, 13, 456), {
|
71 | * year: 'numeric',
|
72 | * month: 'numeric',
|
73 | * day: 'numeric',
|
74 | * hour: 'numeric',
|
75 | * })
|
76 | * //=> 10/4/2019, 12 PM
|
77 | */
|
78 |
|
79 | /**
|
80 | * @param date - The date to format
|
81 | * @param formatOptions - The format options
|
82 | * @param localeOptions - An object with locale
|
83 | *
|
84 | * @returns The formatted date string
|
85 | *
|
86 | * @throws `date` must not be Invalid Date
|
87 | *
|
88 | * @example
|
89 | * // Represent 4 October 2019 in German.
|
90 | * // Convert the date with format's options and locale's options.
|
91 | * const result = intlFormat(new Date(2019, 9, 4, 12, 30, 13, 456), {
|
92 | * weekday: 'long',
|
93 | * year: 'numeric',
|
94 | * month: 'long',
|
95 | * day: 'numeric',
|
96 | * }, {
|
97 | * locale: 'de-DE',
|
98 | * })
|
99 | * //=> Freitag, 4. Oktober 2019
|
100 | */
|
101 |
|
102 | export function intlFormat(date, formatOrLocale, localeOptions) {
|
103 | let formatOptions;
|
104 |
|
105 | if (isFormatOptions(formatOrLocale)) {
|
106 | formatOptions = formatOrLocale;
|
107 | } else {
|
108 | localeOptions = formatOrLocale;
|
109 | }
|
110 |
|
111 | return new Intl.DateTimeFormat(localeOptions?.locale, formatOptions).format(
|
112 | toDate(date),
|
113 | );
|
114 | }
|
115 |
|
116 | function isFormatOptions(opts) {
|
117 | return opts !== undefined && !("locale" in opts);
|
118 | }
|
119 |
|
120 | // Fallback for modularized imports:
|
121 | export default intlFormat;
|