formatDuration.d.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import type { Duration, DurationUnit, LocalizedOptions } from "./types.js";
  2. /**
  3. * The {@link formatDuration} function options.
  4. */
  5. export interface FormatDurationOptions
  6. extends LocalizedOptions<"formatDistance"> {
  7. /** The array of units to format */
  8. format?: DurationUnit[];
  9. /** Should be zeros be included in the output? */
  10. zero?: boolean;
  11. /** The delimiter string to use */
  12. delimiter?: string;
  13. }
  14. /**
  15. * @name formatDuration
  16. * @category Common Helpers
  17. * @summary Formats a duration in human-readable format
  18. *
  19. * @description
  20. * Return human-readable duration string i.e. "9 months 2 days"
  21. *
  22. * @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).
  23. *
  24. * @param duration - The duration to format
  25. * @param options - An object with options.
  26. *
  27. * @returns The formatted date string
  28. *
  29. * @example
  30. * // Format full duration
  31. * formatDuration({
  32. * years: 2,
  33. * months: 9,
  34. * weeks: 1,
  35. * days: 7,
  36. * hours: 5,
  37. * minutes: 9,
  38. * seconds: 30
  39. * })
  40. * //=> '2 years 9 months 1 week 7 days 5 hours 9 minutes 30 seconds'
  41. *
  42. * @example
  43. * // Format partial duration
  44. * formatDuration({ months: 9, days: 2 })
  45. * //=> '9 months 2 days'
  46. *
  47. * @example
  48. * // Customize the format
  49. * formatDuration(
  50. * {
  51. * years: 2,
  52. * months: 9,
  53. * weeks: 1,
  54. * days: 7,
  55. * hours: 5,
  56. * minutes: 9,
  57. * seconds: 30
  58. * },
  59. * { format: ['months', 'weeks'] }
  60. * ) === '9 months 1 week'
  61. *
  62. * @example
  63. * // Customize the zeros presence
  64. * formatDuration({ years: 0, months: 9 })
  65. * //=> '9 months'
  66. * formatDuration({ years: 0, months: 9 }, { zero: true })
  67. * //=> '0 years 9 months'
  68. *
  69. * @example
  70. * // Customize the delimiter
  71. * formatDuration({ years: 2, months: 9, weeks: 3 }, { delimiter: ', ' })
  72. * //=> '2 years, 9 months, 3 weeks'
  73. */
  74. export declare function formatDuration(
  75. duration: Duration,
  76. options?: FormatDurationOptions,
  77. ): string;