Skip to content

Commit

Permalink
Made formatDistanceStrict consider unit option for "12 months" (#2411)
Browse files Browse the repository at this point in the history
Fixed: Made `formatDistanceStrict` return `12 months` instead of `1 year` when `unit: 'month'`.
  • Loading branch information
tan75 committed Apr 14, 2021
1 parent 98db2bc commit c50cb0f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/formatDistanceStrict/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ export default function formatDistanceStrict(
// 1 up to 12 months
} else if (unit === 'month') {
var months = roundingMethodFn(dstNormalizedMinutes / MINUTES_IN_MONTH)
return months === 12
return months === 12 && options.unit !== 'month'
? locale.formatDistance('xYears', 1, localizeOptions)
: locale.formatDistance('xMonths', months, localizeOptions)

Expand Down
9 changes: 9 additions & 0 deletions src/formatDistanceStrict/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,15 @@ describe('formatDistanceStrict', function () {
assert(result === '4 months')
})

it('12 months - see issue 2388', function () {
var result = formatDistanceStrict(
new Date(1986, 7, 4, 10, 32, 0),
new Date(1985, 7, 4, 10, 32, 0),
{ unit: 'month' }
)
assert(result === '12 months')
})

it('24 months', function () {
var result = formatDistanceStrict(
new Date(1986, 3, 4, 10, 32, 0),
Expand Down

0 comments on commit c50cb0f

Please sign in to comment.