fix: make relative dates more compact
This commit is contained in:
1 parent
53227f799e
commit
e54031faae
1 file changed
+5
-4
@@ -1,6 +1,10 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
export let date: Date
|
export let date: Date
|
||||||
export let relativeTo: Date | undefined = undefined
|
export let relativeTo: Date | undefined = undefined
|
||||||
|
export let options: Intl.RelativeTimeFormatOptions = {
|
||||||
|
numeric: 'auto',
|
||||||
|
style: 'narrow',
|
||||||
|
}
|
||||||
|
|
||||||
function formatRelativeDate(date: Date) {
|
function formatRelativeDate(date: Date) {
|
||||||
const now = relativeTo?.getTime() ?? Date.now()
|
const now = relativeTo?.getTime() ?? Date.now()
|
||||||
@@ -21,10 +25,7 @@
|
|||||||
if (diffInMillis >= thresholds[i].threshold) {
|
if (diffInMillis >= thresholds[i].threshold) {
|
||||||
const value = Math.round(diffInMillis / thresholds[i].threshold)
|
const value = Math.round(diffInMillis / thresholds[i].threshold)
|
||||||
|
|
||||||
const rtf = new Intl.RelativeTimeFormat('en', {
|
const rtf = new Intl.RelativeTimeFormat('en', options)
|
||||||
numeric: 'always',
|
|
||||||
style: 'long',
|
|
||||||
})
|
|
||||||
|
|
||||||
return rtf.format(-value, thresholds[i].unit as any)
|
return rtf.format(-value, thresholds[i].unit as any)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user