UNPKG

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