get-date-formatter.d.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { DateFormatError } from './options.js';
  2. import { DateToken } from './tokens.js';
  3. /**
  4. * Returns a date formatter function for the given locales and date skeleton
  5. *
  6. * @remarks
  7. * Uses `Intl.DateTimeFormat` internally.
  8. *
  9. * @public
  10. * @param locales - One or more valid BCP 47 language tags, e.g. `fr` or `en-CA`
  11. * @param tokens - An ICU DateFormat skeleton string, or an array or parsed
  12. * `DateToken` tokens
  13. * @param onError - If defined, will be called separately for each encountered
  14. * parsing error and unsupported feature.
  15. * @example
  16. * ```js
  17. * import { getDateFormatter } from '@messageformat/date-skeleton'
  18. *
  19. * // 2006 Jan 2, 15:04:05.789 in local time
  20. * const date = new Date(2006, 0, 2, 15, 4, 5, 789)
  21. *
  22. * let fmt = getDateFormatter('en-CA', 'GrMMMdd', console.error)
  23. * fmt(date) // 'Jan. 02, 2006 AD'
  24. *
  25. * fmt = getDateFormatter('en-CA', 'hamszzzz', console.error)
  26. * fmt(date) // '3:04:05 p.m. Newfoundland Daylight Time'
  27. * ```
  28. */
  29. export declare function getDateFormatter(locales: string | string[], tokens: string | DateToken[], onError?: (error: DateFormatError) => void): (date: Date | number) => string;
  30. /**
  31. * Returns a string of JavaScript source that evaluates to a date formatter
  32. * function with the same `(date: Date | number) => string` signature as the
  33. * function returned by {@link getDateFormatter}.
  34. *
  35. * @remarks
  36. * The returned function will memoize an `Intl.DateTimeFormat` instance.
  37. *
  38. * @public
  39. * @param locales - One or more valid BCP 47 language tags, e.g. `fr` or `en-CA`
  40. * @param tokens - An ICU DateFormat skeleton string, or an array or parsed
  41. * `DateToken` tokens
  42. * @param onError - If defined, will be called separately for each encountered
  43. * parsing error and unsupported feature.
  44. * @example
  45. * ```js
  46. * import { getDateFormatterSource } from '@messageformat/date-skeleton'
  47. *
  48. * getDateFormatterSource('en-CA', 'GrMMMdd', console.error)
  49. * // '(function() {\n' +
  50. * // ' var opt = {"era":"short","calendar":"gregory","year":"numeric",' +
  51. * // '"month":"short","day":"2-digit"};\n' +
  52. * // ' var dtf = new Intl.DateTimeFormat("en-CA", opt);\n' +
  53. * // ' return function(value) { return dtf.format(value); }\n' +
  54. * // '})()'
  55. *
  56. * const src = getDateFormatterSource('en-CA', 'hamszzzz', console.error)
  57. * // '(function() {\n' +
  58. * // ' var opt = {"hour":"numeric","hourCycle":"h12","minute":"numeric",' +
  59. * // '"second":"numeric","timeZoneName":"long"};\n' +
  60. * // ' var dtf = new Intl.DateTimeFormat("en-CA", opt);\n' +
  61. * // ' return function(value) { return dtf.format(value); }\n' +
  62. * // '})()'
  63. *
  64. * const fmt = new Function(`return ${src}`)()
  65. * const date = new Date(2006, 0, 2, 15, 4, 5, 789)
  66. * fmt(date) // '3:04:05 p.m. Newfoundland Daylight Time'
  67. * ```
  68. */
  69. export declare function getDateFormatterSource(locales: string | string[], tokens: string | DateToken[], onError?: (err: DateFormatError) => void): string;