UNPKG

1.83 kBTypeScriptView Raw
1import type { Duration, DurationUnit, LocalizedOptions } from "./types.js";
2/**
3 * The {@link formatDuration} function options.
4 */
5export 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 * @param duration - The duration to format
23 * @param options - An object with options.
24 *
25 * @returns The formatted date string
26 *
27 * @example
28 * // Format full duration
29 * formatDuration({
30 * years: 2,
31 * months: 9,
32 * weeks: 1,
33 * days: 7,
34 * hours: 5,
35 * minutes: 9,
36 * seconds: 30
37 * })
38 * //=> '2 years 9 months 1 week 7 days 5 hours 9 minutes 30 seconds'
39 *
40 * @example
41 * // Format partial duration
42 * formatDuration({ months: 9, days: 2 })
43 * //=> '9 months 2 days'
44 *
45 * @example
46 * // Customize the format
47 * formatDuration(
48 * {
49 * years: 2,
50 * months: 9,
51 * weeks: 1,
52 * days: 7,
53 * hours: 5,
54 * minutes: 9,
55 * seconds: 30
56 * },
57 * { format: ['months', 'weeks'] }
58 * ) === '9 months 1 week'
59 *
60 * @example
61 * // Customize the zeros presence
62 * formatDuration({ years: 0, months: 9 })
63 * //=> '9 months'
64 * formatDuration({ years: 0, months: 9 }, { zero: true })
65 * //=> '0 years 9 months'
66 *
67 * @example
68 * // Customize the delimiter
69 * formatDuration({ years: 2, months: 9, weeks: 3 }, { delimiter: ', ' })
70 * //=> '2 years, 9 months, 3 weeks'
71 */
72export declare function formatDuration(
73 duration: Duration,
74 options?: FormatDurationOptions,
75): string;