Skip to content

Commit 8836869

Browse files
authored
Merge pull request #6 from atlassian/issue/add-formatDurationByOptions
Add formatDurationByOptions
2 parents 2e6594c + 79a5e28 commit 8836869

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ console.log(formattedDateTimeWithOptions); // Outputs: "Jan 1, 2024, 12:00 PM"
5858
// Format a duration
5959
const duration = dateTime.formatDuration(new Date(2024, 0, 1), new Date(2024, 0, 2), 'en-US');
6060
console.log(duration); // Outputs: "1 day 1 hour 1 minute 1 second"
61+
62+
// Format a duration with custom options
63+
const durationWithOptions = dateTime.formatDurationByOptions(
64+
{ unitDisplay: 'narrow' },
65+
new Date(2024, 0, 1),
66+
new Date(2024, 0, 2),
67+
'en-US'
68+
);
69+
console.log(durationWithOptions); // Outputs: "1d 1h 1m 1s"
6170
```
6271

6372
## Installation

src/index.test.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,4 +415,43 @@ describe('date-time', () => {
415415
test('should not format duration', () => {
416416
expect(dateTime.formatDuration(new Date(2000, 0, 2), new Date(2000, 0, 1), 'en-US')).toStrictEqual('');
417417
});
418-
});
418+
419+
describe('formatDurationByOptions', () => {
420+
const baseDate = new Date('2024-01-01T00:00:00Z');
421+
const laterDate = new Date('2024-01-01T01:02:03Z');
422+
423+
test('with default options', () => {
424+
const options: Intl.NumberFormatOptions = {
425+
unitDisplay: 'long',
426+
}
427+
expect(dateTime.formatDurationByOptions(options, baseDate, laterDate)).toBe('1 hour 2 minutes 3 seconds');
428+
});
429+
430+
test('with compact notation', () => {
431+
const options: Intl.NumberFormatOptions = {
432+
notation: 'compact',
433+
style: 'unit'
434+
};
435+
expect(dateTime.formatDurationByOptions(options, baseDate, laterDate)).toBe('1 hr 2 min 3 sec');
436+
});
437+
438+
test('with different unit style', () => {
439+
const options: Intl.NumberFormatOptions = {
440+
style: 'unit',
441+
unitDisplay: 'narrow'
442+
};
443+
expect(dateTime.formatDurationByOptions(options, baseDate, laterDate)).toBe('1h 2m 3s');
444+
});
445+
446+
test('with different locale', () => {
447+
const options: Intl.NumberFormatOptions = {
448+
unitDisplay: 'narrow',
449+
}
450+
expect(dateTime.formatDurationByOptions(options, baseDate, laterDate, 'en-US')).toBe('1h 2m 3s');
451+
expect(dateTime.formatDurationByOptions(options, baseDate, laterDate, 'zh-CN')).toBe('1小时 2分钟 3秒');
452+
expect(dateTime.formatDurationByOptions(options, baseDate, laterDate, 'ja-JP')).toBe('1h 2m 3s');
453+
expect(dateTime.formatDurationByOptions(options, baseDate, laterDate, 'ko-KR')).toBe('1시간 2분 3초');
454+
expect(dateTime.formatDurationByOptions(options, baseDate, laterDate, 'de-DE')).toBe('1 Std. 2 Min. 3 Sek.');
455+
});
456+
});
457+
});

src/index.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,19 @@ export function formatDateTimeByOptions(options: Intl.DateTimeFormatOptions, dat
170170
}
171171

172172
export function formatDuration(from: Date, to = new Date(), locale = getLocale()) {
173+
return formatDurationByOptions({ unitDisplay: 'long' }, from, to, locale);
174+
}
175+
176+
export function formatDurationByOptions(options: Intl.NumberFormatOptions, from: Date, to = new Date(), locale = getLocale()) {
177+
if (!options) {
178+
throw new Error('Please use formatDuration instead');
179+
}
180+
173181
const milliseconds = to.getTime() - from.getTime();
174182
if (milliseconds < 0) {
175183
return '';
176184
}
185+
177186
const dayInMs = 24 * 60 * 60 * 1000;
178187
const hourInMs = 60 * 60 * 1000;
179188
const minuteInMs = 60 * 1000;
@@ -187,8 +196,8 @@ export function formatDuration(from: Date, to = new Date(), locale = getLocale()
187196
const getNumberFormat = (unit: string, number: number | bigint) =>
188197
new Intl.NumberFormat(locale, {
189198
style: 'unit',
190-
unitDisplay: 'long',
191199
unit,
200+
...options,
192201
}).format(number);
193202

194203
let result = '';
@@ -204,4 +213,4 @@ export function formatDuration(from: Date, to = new Date(), locale = getLocale()
204213

205214
result = `${result ? `${result} ` : ''}${getNumberFormat('second', seconds)}`;
206215
return result;
207-
}
216+
}

0 commit comments

Comments
 (0)