:raised_hands: A vue component used to format date with time ago statement. :speech_balloon:
just now
** minutes ago
** hours ago
** days ago
yyyy-mm-dd hh:mm
yarn
yarn serve
Get from npm / yarn:
npm i [email protected]
yarn add [email protected]
or just include vue2-timeago.umd.min.js to your view like
<script src='https://cdn.jsdelivr.net/npm/[email protected]/dist/vue2-timeago.umd.min.js'></script>
Use this inside your app:
import { TimeAgo } from 'vue2-timeago'
export default {
name: 'app',
components: {
TimeAgo,
}
}
import 'vue2-timeago/dist/vue2-timeago.css'
or just include vue2-timeago.css
<time-ago :refresh="60" :datetime="new Date(2018, 7, 4, 0, 24, 0)" locale="zh_TW" tooltip></time-ago>
Default locale is en, and the library supports en and zh_TW.
<time-ago locale="en"></time-ago>
<time-ago :locale="locale"></time-ago> use v-bind
export default {
...
data(){
return{
locale:"zh_TW",
}
},
...
<time-ago datetime="2018-08-03 15:47:00"></time-ago>
<time-ago :datetime="new Date(2018, 7, 4, 0, 24, 0)"></time-ago> use v-bind
<time-ago :datetime="1533286641826"></time-ago> timestamp
Note. Don't bind with new Date()
when you use refresh property.
Because every time refresh will get a new date value.
<time-ago :datetime="new Date(2018, 7, 4, 0, 24, 0)"></time-ago> --> OK
<time-ago refresh :datetime="new Date()"></time-ago> --> not OK
If you want use new Date() , just remove datetime property.
<time-ago refresh></time-ago>
<time-ago refresh></time-ago> Boolean , default refresh time 60/s
<time-ago :refresh="3600"></time-ago> bind value with a number
<time-ago :refresh="1"></time-ago> Refresh time 1 second
<time-ago tooltip></time-ago> Show tooltip
<time-ago :tooltip="false"></time-ago> Disabled tooltip
{
placement: 'top',
content: {datetime string}
}
<time-ago tooltip :tooltip-options="tooltipOptions"/>
data () {
return {
tooltipOptions: {
placement: 'bottom',
}
}
}
<time-ago :datetime="datetime"></time-ago> show : 2d
<time-ago :datetime="datetime" long></time-ago> show : 2 days ago
You can do something when time refresh every time
<time-ago :refresh="1" :locale="locale" @update="timeRefresh" />
Parameters example:
methods: {
timeRefresh({ timeago, nowString, timestamp }) {
console.log(timeago) // 51m
console.log(nowString) // 2021-01-21 10:55
console.log(timestamp) // Thu Jan 21 2021 10:55:18 GMT+0800 (台北標準時間)
}
}
<time-ago @click="todo"><time-ago/>
<time-ago @mouseover="todo"><time-ago/>
Property | Type | Default | Description |
---|---|---|---|
datetime | Date, String, Number | new Date() | The datetime to be formatted. |
locale | String | en | message language |
refresh | Boolean, Number | false | The period to update the component, in seconds. When true it will be 60s. Also you can bind a number. |
long | Boolean | false | Show long string with time message . ex. 3h -> 3 hours age |
tooltip | Boolean | false | Show tooltip. |
tooltip-options | Object | { placement: 'top', content: #datetime string } | tooltip options |
Property | Description |
---|---|
update | After timer refreshed will trigger this event |
To install in nuxt run
npm i vue2-timeago
In nuxt.config.js
add
plugins: [
{ src: '~/plugins/vue2-timeago' }
],
In plugins/vue2-timeago.js
type:
import Vue from 'vue';
import { TimeAgo } from 'vue2-timeago';
import 'vue2-timeago/dist/vue2-timeago.css';
Vue.component('time-ago', TimeAgo);
Then in components use as:
<time-ago :datetime="new Date()" tooltip/>
locale translations: The component needs more locale translations. You can Open an issue to write the locale translations, or submit a pull request
.
See example here.
locale support list :
Thanks for help: