intlFormat.d.ts 4.4 KB

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