index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import requiredArgs from "../_lib/requiredArgs/index.js";
  2. /**
  3. * @name intlFormat
  4. * @category Common Helpers
  5. * @summary Format the date with Intl.DateTimeFormat (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat).
  6. *
  7. * @description
  8. * Return the formatted date string in the given format.
  9. * The method uses [`Intl.DateTimeFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat) inside.
  10. * formatOptions are the same as [`Intl.DateTimeFormat` options](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat#using_options)
  11. *
  12. * > ⚠️ Please note that before Node version 13.0.0, only the locale data for en-US is available by default.
  13. *
  14. * @param {Date|Number} argument - the original date.
  15. * @param {Object} [formatOptions] - an object with options.
  16. * @param {'lookup'|'best fit'} [formatOptions.localeMatcher='best fit'] - locale selection algorithm.
  17. * @param {'narrow'|'short'|'long'} [formatOptions.weekday] - representation the days of the week.
  18. * @param {'narrow'|'short'|'long'} [formatOptions.era] - representation of eras.
  19. * @param {'numeric'|'2-digit'} [formatOptions.year] - representation of years.
  20. * @param {'numeric'|'2-digit'|'narrow'|'short'|'long'} [formatOptions.month='numeric'] - representation of month.
  21. * @param {'numeric'|'2-digit'} [formatOptions.day='numeric'] - representation of day.
  22. * @param {'numeric'|'2-digit'} [formatOptions.hour='numeric'] - representation of hours.
  23. * @param {'numeric'|'2-digit'} [formatOptions.minute] - representation of minutes.
  24. * @param {'numeric'|'2-digit'} [formatOptions.second] - representation of seconds.
  25. * @param {'short'|'long'} [formatOptions.timeZoneName] - representation of names of time zones.
  26. * @param {'basic'|'best fit'} [formatOptions.formatMatcher='best fit'] - format selection algorithm.
  27. * @param {Boolean} [formatOptions.hour12] - determines whether to use 12-hour time format.
  28. * @param {String} [formatOptions.timeZone] - the time zone to use.
  29. * @param {Object} [localeOptions] - an object with locale.
  30. * @param {String|String[]} [localeOptions.locale] - the locale code
  31. * @returns {String} the formatted date string.
  32. * @throws {TypeError} 1 argument required.
  33. * @throws {RangeError} `date` must not be Invalid Date
  34. *
  35. * @example
  36. * // Represent 10 October 2019 in German.
  37. * // Convert the date with format's options and locale's options.
  38. * const result = intlFormat(new Date(2019, 9, 4, 12, 30, 13, 456), {
  39. * weekday: 'long',
  40. * year: 'numeric',
  41. * month: 'long',
  42. * day: 'numeric',
  43. * }, {
  44. * locale: 'de-DE',
  45. * })
  46. * //=> Freitag, 4. Oktober 2019
  47. *
  48. * @example
  49. * // Represent 10 October 2019.
  50. * // Convert the date with format's options.
  51. * const result = intlFormat.default(new Date(2019, 9, 4, 12, 30, 13, 456), {
  52. * year: 'numeric',
  53. * month: 'numeric',
  54. * day: 'numeric',
  55. * hour: 'numeric',
  56. * })
  57. * //=> 10/4/2019, 12 PM
  58. *
  59. * @example
  60. * // Represent 10 October 2019 in Korean.
  61. * // Convert the date with locale's options.
  62. * const result = intlFormat(new Date(2019, 9, 4, 12, 30, 13, 456), {
  63. * locale: 'ko-KR',
  64. * })
  65. * //=> 2019. 10. 4.
  66. *
  67. * @example
  68. * // Represent 10 October 2019 in middle-endian format:
  69. * const result = intlFormat(new Date(2019, 9, 4, 12, 30, 13, 456))
  70. * //=> 10/4/2019
  71. */
  72. export default function intlFormat(date, formatOrLocale, localeOptions) {
  73. var _localeOptions;
  74. requiredArgs(1, arguments);
  75. var formatOptions;
  76. if (isFormatOptions(formatOrLocale)) {
  77. formatOptions = formatOrLocale;
  78. } else {
  79. localeOptions = formatOrLocale;
  80. }
  81. return new Intl.DateTimeFormat((_localeOptions = localeOptions) === null || _localeOptions === void 0 ? void 0 : _localeOptions.locale, formatOptions).format(date);
  82. }
  83. function isFormatOptions(opts) {
  84. return opts !== undefined && !('locale' in opts);
  85. }