intlFormat.js 3.3 KB

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