-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmoments-ago.vue
More file actions
127 lines (119 loc) · 2.67 KB
/
moments-ago.vue
File metadata and controls
127 lines (119 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<template>
<span v-if="date" class="vue-moments-ago"
>{{ prefix }} {{ humanFormatted }} {{ suffix }}</span
>
</template>
<script>
import moment from "moment";
export default {
data() {
return {
language: {
en: {
few: "a few",
postfix: "s"
},
kr: {
few: "몇",
postfix: ""
},
jp: {
few: "何",
postfix: ""
},
he: {
few: "כמה",
postfix: "",
}
},
epochs: {
en: ["year", "month", "day", "hour", "minute"],
kr: ["년", "달", "일", "시간", "분"],
jp: ["年", "月", "日", "時", "分"],
he: ["שנה", "חודש", "יום", "שעה", "דקה"]
},
year: 31536000,
month: 2592000,
day: 86400,
hour: 3600,
minute: 60,
humanReadable: "",
humanDifference: 0,
humanWord: "moment"
};
},
props: {
prefix: {
type: String,
default: "posted"
},
suffix: {
type: String,
default: "ago"
},
date: {
type: String,
required: true
},
lang: {
type: String,
default: "en"
}
},
created() {
this.updateDifference();
const interval = setInterval(() => {
this.updateDifference();
}, 1000);
this.$once('hook:destroyed', () => clearInterval(interval));
},
computed: {
humanFormatted() {
if (this.humanDifference === 0) {
return (
this.language[this.lang].few +
" " +
this.humanWord +
this.language[this.lang].postfix
);
} else if (this.humanDifference > 1) {
return (
this.humanDifference +
" " +
this.humanWord +
this.language[this.lang].postfix
);
} else {
return this.humanDifference + " " + this.humanWord;
}
}
},
methods: {
updateDifference() {
let seconds = moment().diff(moment(this.date), "seconds");
this.humanReadable = this.getDuration(seconds);
if (this.humanReadable) {
this.humanDifference = this.humanReadable.interval;
this.humanWord = this.humanReadable.humanEpoch;
}
},
getDuration(seconds) {
let epoch, interval;
let humanEpoch;
for (let i = 0; i < this.epochs[this.lang].length; i++) {
epoch = this.epochs.en[i];
humanEpoch = this.epochs[this.lang][i];
interval = Math.floor(seconds / this[epoch]);
if (interval >= 1) {
return { interval: interval, humanEpoch: humanEpoch };
}
}
}
}
};
</script>
<style scoped>
.vue-moments-ago {
font-size: 12px;
}
</style>