Skip to content

Commit ad7c3be

Browse files
authored
Merge pull request #133 from github/changes-to-last-year-format
Changes to last year format
2 parents 2b29c33 + ce58135 commit ad7c3be

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-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: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
suite('time-ago', function() {
2+
let dateNow
3+
4+
function freezeTime(date) {
5+
dateNow = Date.now
6+
Date.now = function() {
7+
return date
8+
}
9+
}
10+
11+
teardown(function() {
12+
if (dateNow) {
13+
Date.now = dateNow
14+
}
15+
})
16+
217
test('always uses relative dates', function() {
318
const now = new Date(Date.now() - 10 * 365 * 24 * 60 * 60 * 1000).toISOString()
419
const time = document.createElement('time-ago')
@@ -44,11 +59,20 @@ suite('time-ago', function() {
4459
assert.equal(time.textContent, '3 months ago')
4560
})
4661

47-
test('rewrites from now past datetime to years ago', function() {
48-
const now = new Date(Date.now() - 12 * 30 * 24 * 60 * 60 * 1000).toISOString()
49-
const time = document.createElement('time-ago')
50-
time.setAttribute('datetime', now)
51-
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')
5276
})
5377

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

0 commit comments

Comments
 (0)