Skip to content

Commit ce58135

Browse files
committed
display months ago for time <= 18 months ago
1 parent 5cc9e00 commit ce58135

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

src/relative-time.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,8 @@ export default class RelativeTime {
8181
return formatRelativeTime(this.locale, -day, 'day')
8282
} else if (day < 30) {
8383
return formatRelativeTime(this.locale, -day, 'day')
84-
} else if (month < 12) {
85-
return formatRelativeTime(this.locale, -month, 'month')
8684
} else if (month < 18) {
87-
return formatRelativeTime(this.locale, -year, 'year')
85+
return formatRelativeTime(this.locale, -month, 'month')
8886
} else {
8987
return formatRelativeTime(this.locale, -year, 'year')
9088
}

test/time-ago.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,20 @@ suite('time-ago', function() {
5959
assert.equal(time.textContent, '3 months ago')
6060
})
6161

62-
test('rewrites from now past datetime to years ago', function() {
63-
const now = new Date(Date.now() - 12 * 30 * 24 * 60 * 60 * 1000).toISOString()
64-
const time = document.createElement('time-ago')
65-
time.setAttribute('datetime', now)
66-
assert.equal(time.textContent, 'last year')
62+
test('rewrites time-ago datetimes < 18months as "months ago"', function() {
63+
freezeTime(new Date(2020, 0, 1))
64+
const then = new Date(2018, 10, 1).toISOString()
65+
const timeElement = document.createElement('time-ago')
66+
timeElement.setAttribute('datetime', then)
67+
assert.equal(timeElement.textContent, '15 months ago')
68+
})
69+
70+
test('rewrites time-ago datetimes >= 18 months as "years ago"', function() {
71+
freezeTime(new Date(2020, 0, 1))
72+
const then = new Date(2018, 6, 1).toISOString()
73+
const timeElement = document.createElement('time-ago')
74+
timeElement.setAttribute('datetime', then)
75+
assert.equal(timeElement.textContent, '2 years ago')
6776
})
6877

6978
test('micro formats years', function() {

0 commit comments

Comments
 (0)