Vue Timeago Component

5.1.3 · maintenance · verified Sun Apr 19

The `vue-timeago` package provides a Vue.js component for displaying time in a human-readable "time ago" format, such as "5 minutes ago" or "2 days ago". The current stable version is 5.1.3, released in January 2021, indicating a slower release cadence, likely in a maintenance state for its existing feature set. It integrates with `date-fns` under the hood (since version 5.0.0) for precise date calculations and extensive locale support, which differentiates it from prior versions (like v3 and v4) that were much smaller but potentially less robust. It functions as a Vue plugin, exposing a `<timeago>` component that accepts a `datetime` prop and offers auto-updating capabilities, custom locales, and converter functions. While it leverages `date-fns` for its core logic, this dependency means a larger bundle size compared to its predecessors. It is primarily designed for Vue 2 applications, with dedicated alternatives available for Vue 3.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to install `vue-timeago` as a Vue plugin, register custom `date-fns` locales, and use the `<timeago>` component with various props, including auto-update and custom locales, for both past and future dates.

import Vue from 'vue'
import VueTimeago from 'vue-timeago'
import zhCNLocale from 'date-fns/locale/zh_cn'
import jaLocale from 'date-fns/locale/ja'

// Register the plugin globally
Vue.use(VueTimeago, {
  name: 'Timeago', // Component name
  locale: 'en', // Default locale
  locales: {
    'zh-CN': zhCNLocale,
    ja: jaLocale
  }
})

// Example Vue component usage
export default {
  name: 'App',
  data() {
    return {
      // Use a past date for a clear 'time ago' display
      pastTime: new Date(2023, 10, 15, 10, 30, 0), // November 15, 2023, 10:30:00
      // Use a future date to show 'in X time'
      futureTime: new Date(Date.now() + 1000 * 60 * 60 * 24 * 5) // 5 days from now
    }
  },
  template: `
    <div id="app">
      <h1>Time Ago Examples</h1>
      <p>Simple usage: <timeago :datetime="pastTime"></timeago></p>
      <p>Auto-update every 30 seconds: <timeago :datetime="pastTime" :auto-update="30"></timeago></p>
      <p>Custom locale (Chinese): <timeago :datetime="pastTime" locale="zh-CN"></timeago></p>
      <p>Future date: <timeago :datetime="futureTime"></timeago></p>
    </div>
  `
}

view raw JSON →