Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom format #177

Merged
merged 5 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ <h2>Future Date</h2>
</relative-time>
</p>

<p>
Relative time with custom format:
<relative-time datetime="2017-01-01T00:00:00.000Z" format="%Y-%m-%d">
Oops! This browser doesn't support Web Components.
</relative-time>
</p>

<p>
Time until:
<time-until datetime="2024-12-31T00:00:00-05:00">
Expand Down
2 changes: 1 addition & 1 deletion src/relative-time-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default class RelativeTimeElement extends ExtendedTimeElement {
getFormattedDate(): string | undefined {
const date = this.date
if (!date) return
return new RelativeTime(date, localeFromElement(this)).toString()
return new RelativeTime(date, localeFromElement(this)).toString(this.getAttribute('format') || undefined)
}

connectedCallback(): void {
Expand Down
28 changes: 16 additions & 12 deletions src/relative-time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ export default class RelativeTime {
this.locale = locale
}

toString(): string {
toString(format?: string): string {
const ago = this.timeElapsed()
if (ago) {
return ago
} else {
const ahead = this.timeAhead()
if (ahead) {
return ahead
} else {
return `on ${this.formatDate()}`
}
}
const ahead = this.timeAhead()
if (ahead) {
return ahead
}
if (format) {
return this.formatDate(format)
}
return `on ${this.formatDate()}`
}

timeElapsed(): string | undefined | null {
Expand Down Expand Up @@ -167,10 +168,13 @@ export default class RelativeTime {
}
}

formatDate(): string {
let format = isDayFirst(this.locale) ? '%e %b' : '%b %e'
if (!isThisYear(this.date)) {
format += isYearSeparator() ? ', %Y' : ' %Y'
formatDate(defaultFormat?: string): string {
let format = defaultFormat
if (format == null) {
format = isDayFirst() ? '%e %b' : '%b %e'
if (!isThisYear(this.date)) {
format += isYearSeparator() ? ', %Y' : ' %Y'
}
}
return strftime(this.date, format)
}
Expand Down
17 changes: 17 additions & 0 deletions test/relative-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,23 @@ suite('relative-time', function () {
assert.equal(root.children[0].textContent, 'now')
})

test('allows for use of custom formats', function () {
const time = document.createElement('relative-time')
time.textContent = 'Jun 30'
time.setAttribute('datetime', '2022-01-10T12:00:00')
time.setAttribute('format', '%Y')
assert.equal(time.textContent, '2022')
})

test('ignores blank formats', function () {
const time = document.createElement('relative-time')
time.textContent = 'Jun 30'
time.setAttribute('datetime', '2022-01-10T12:00:00')
time.setAttribute('lang', 'en-US')
time.setAttribute('format', '')
assert.equal(time.textContent, 'on Jan 10')
})

const esLangSupport = (function () {
try {
return new Intl.RelativeTimeFormat('es').format(1, 'minute') === 'dentro de 1 minuto'
Expand Down