add distance date
This commit is contained in:
24
src/components/DistanceDate.svelte
Normal file
24
src/components/DistanceDate.svelte
Normal file
@ -0,0 +1,24 @@
|
||||
<script context="module">
|
||||
import { formatDistance } from 'date-fns'
|
||||
import { readable } from 'svelte/store'
|
||||
|
||||
const format = (date, baseDate) => formatDistance(date, baseDate, { addSuffix: true })
|
||||
|
||||
const now = readable(new Date(), set => {
|
||||
const interval = setInterval(() => {
|
||||
set(new Date())
|
||||
}, 1000)
|
||||
|
||||
return () => clearInterval(interval)
|
||||
})
|
||||
</script>
|
||||
|
||||
<script>
|
||||
export let date
|
||||
|
||||
$: iso = date.toISOString()
|
||||
$: text = format(date, $now)
|
||||
</script>
|
||||
|
||||
<time datetime={iso}>{text}</time>
|
||||
|
Reference in New Issue
Block a user