yandere_fe/src/components/timeago/timeago.vue

54 lines
1.4 KiB
Vue
Raw Normal View History

2019-06-18 13:28:31 -07:00
<template>
2019-07-05 00:17:44 -07:00
<time
:datetime="time"
:title="localeDateString"
>
2019-06-18 13:28:31 -07:00
{{ $t(relativeTime.key, [relativeTime.num]) }}
</time>
</template>
<script>
import * as DateUtils from 'src/services/date_utils/date_utils.js'
import localeService from 'src/services/locale/locale.service.js'
2019-06-18 13:28:31 -07:00
export default {
name: 'Timeago',
props: ['time', 'autoUpdate', 'longFormat', 'nowThreshold'],
data () {
return {
relativeTime: { key: 'time.now', num: 0 },
interval: null
}
},
computed: {
localeDateString () {
const browserLocale = localeService.internalToBrowserLocale(this.$i18n.locale)
2019-06-18 13:28:31 -07:00
return typeof this.time === 'string'
? new Date(Date.parse(this.time)).toLocaleString(browserLocale)
: this.time.toLocaleString(browserLocale)
2019-06-18 13:28:31 -07:00
}
},
2019-07-05 00:17:44 -07:00
created () {
this.refreshRelativeTimeObject()
},
destroyed () {
clearTimeout(this.interval)
},
2019-06-18 13:28:31 -07:00
methods: {
refreshRelativeTimeObject () {
const nowThreshold = typeof this.nowThreshold === 'number' ? this.nowThreshold : 1
this.relativeTime = this.longFormat
? DateUtils.relativeTime(this.time, nowThreshold)
: DateUtils.relativeTimeShort(this.time, nowThreshold)
if (this.autoUpdate) {
this.interval = setTimeout(
this.refreshRelativeTimeObject,
1000 * this.autoUpdate
)
}
}
}
}
2019-07-05 00:17:44 -07:00
</script>