2021-11-10 10:41:40 -08:00
|
|
|
<template>
|
|
|
|
<span :title="convertDate">
|
|
|
|
{{ format }}
|
2021-07-22 01:22:17 -07:00
|
|
|
</span>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts">
|
2021-09-11 01:15:36 -07:00
|
|
|
import { format, LocaleFunc, register } from 'timeago.js';
|
|
|
|
import { convertToHumanReadableDate } from './helpers';
|
2021-11-10 10:41:40 -08:00
|
|
|
import Vue from 'vue';
|
|
|
|
import { mapGetters } from 'vuex';
|
2021-09-11 01:15:36 -07:00
|
|
|
|
2021-12-07 07:14:40 -08:00
|
|
|
export default Vue.extend({
|
2021-11-10 10:41:40 -08:00
|
|
|
name: 'TimeAgo',
|
2021-09-11 01:15:36 -07:00
|
|
|
props: {
|
|
|
|
date: {
|
|
|
|
type: String,
|
|
|
|
},
|
|
|
|
capitalize: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false,
|
|
|
|
},
|
|
|
|
},
|
2021-11-10 10:41:40 -08:00
|
|
|
beforeMount() {
|
|
|
|
register(this.defaultLocale, this.localeFunc as LocaleFunc);
|
|
|
|
},
|
2021-09-11 01:15:36 -07:00
|
|
|
methods: {
|
2021-11-10 10:41:40 -08:00
|
|
|
localeFunc(num: number, index: number, totalSec: number): [string, string] {
|
|
|
|
// number: the timeago / timein number;
|
|
|
|
// index: the index of array below;
|
|
|
|
// totalSec: total seconds between date to be formatted and today's date;
|
|
|
|
return [
|
2021-12-15 04:16:53 -08:00
|
|
|
[this.$locale.baseText('timeAgo.justNow'), this.$locale.baseText('timeAgo.rightNow')],
|
|
|
|
[this.$locale.baseText('timeAgo.justNow'), this.$locale.baseText('timeAgo.rightNow')], // ['%s seconds ago', 'in %s seconds'],
|
|
|
|
[this.$locale.baseText('timeAgo.oneMinuteAgo'), this.$locale.baseText('timeAgo.inOneMinute')],
|
|
|
|
[this.$locale.baseText('timeAgo.minutesAgo'), this.$locale.baseText('timeAgo.inMinutes')],
|
|
|
|
[this.$locale.baseText('timeAgo.oneHourAgo'), this.$locale.baseText('timeAgo.inOneHour')],
|
|
|
|
[this.$locale.baseText('timeAgo.hoursAgo'), this.$locale.baseText('timeAgo.inHours')],
|
|
|
|
[this.$locale.baseText('timeAgo.oneDayAgo'), this.$locale.baseText('timeAgo.inOneDay')],
|
|
|
|
[this.$locale.baseText('timeAgo.daysAgo'), this.$locale.baseText('timeAgo.inDays')],
|
|
|
|
[this.$locale.baseText('timeAgo.oneWeekAgo'), this.$locale.baseText('timeAgo.inOneWeek')],
|
|
|
|
[this.$locale.baseText('timeAgo.weeksAgo'), this.$locale.baseText('timeAgo.inWeeks')],
|
|
|
|
[this.$locale.baseText('timeAgo.oneMonthAgo'), this.$locale.baseText('timeAgo.inOneMonth')],
|
|
|
|
[this.$locale.baseText('timeAgo.monthsAgo'), this.$locale.baseText('timeAgo.inMonths')],
|
|
|
|
[this.$locale.baseText('timeAgo.oneYearAgo'), this.$locale.baseText('timeAgo.inOneYear')],
|
|
|
|
[this.$locale.baseText('timeAgo.yearsAgo'), this.$locale.baseText('timeAgo.inYears')],
|
2021-11-10 10:41:40 -08:00
|
|
|
][index] as [string, string];
|
|
|
|
},
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
...mapGetters(['defaultLocale']),
|
|
|
|
format(): string {
|
|
|
|
const text = format(this.date, this.defaultLocale);
|
2021-09-11 01:15:36 -07:00
|
|
|
|
2021-11-10 10:41:40 -08:00
|
|
|
if (!this.capitalize) {
|
2021-09-11 01:15:36 -07:00
|
|
|
return text.toLowerCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
return text;
|
|
|
|
},
|
2021-11-10 10:41:40 -08:00
|
|
|
convertDate(): string {
|
|
|
|
const date = new Date(this.date);
|
2022-09-21 01:20:29 -07:00
|
|
|
const epoch = date.getTime();
|
2021-11-10 10:41:40 -08:00
|
|
|
return convertToHumanReadableDate(epoch);
|
|
|
|
},
|
2021-09-11 01:15:36 -07:00
|
|
|
},
|
2021-11-10 10:41:40 -08:00
|
|
|
});
|
2021-09-11 01:15:36 -07:00
|
|
|
</script>
|