intlFormat.mjs 4.0 KB

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