Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A tiny timeago component for Vue 3 using date-fns v2.28.

License

NotificationsYou must be signed in to change notification settings

0x0dr1y/vue-timeago3

Repository files navigation

NPM versionNPM downloadscode-coverageBuild StatusVue versiondate-fns versionsize

A time ago component for Vue.js 3 based onvue-timeago for Vue 2 by egoist.

Table of Contents

vue-timeago3 is a tiny component for Vue.js 3, to show the time passed since a specific date. You simply pass a date and get somewhat like10 seconds ago,3 weeks ago,... printed by the component

Example

distance to nowresult
0 - 5 secsless than 5 seconds ago
5 - 10 secsless than 10 seconds ago
10 - 20 secsless than 20 seconds ago
20 - 40 secshalf a minute ago
40 - 60 secsless than a minute ago

Seedate-fns/formatDistanceToNow for more details.

Visit thedocs for more details!

Currently the plugin is available via NPM and Yarn. To install it use one of the two package managers.

// NPM$npminstallvue-timeago3// Yarn$yarnaddvue-timeago3

To register the plugin simply import and register it using the new global vue3 api. As an alternative the plugin could be imported in specific components only.

// src/main.tsimport{createApp}from'vue'importtimeagofrom'vue-timeago3'constapp=createApp(App)...app.use(timeago)// register timeago...app.mount('#app')

Plugin Options

During the registration of the component you can specify a set of options, which will mutate the pluginglobally. If you don't want to define global settings, skip this section and use props instead. To use options, simply pass them during the registration as an object:

// src/main.tsimport{createApp}from'vue'importtimeagofrom'vue-timeago3'constapp=createApp(App)...// define optionsconsttimeagoOptions={converterOptions:{includeSeconds:false,}}app.use(timeago,timeagoOptions)// register timeago with options...app.mount('#app')

As of version 1.0.0 the following options are available:

optiontypedescription
namestringRegister the component with a custom name. Default is:timeago
localeLocale (seedate-fns/Locale)Thelocale specifies the language which is used to render the date. All availabledate-fns locales are supported by default.
converter(date, defaultConvertOptions | strictConverterOptions) => stringAconverter that formats regular dates inx Seconds ago, or inxxx style. Check out the default converter which usesdate-fns formatDistanceToNow
defaultConverterOptionsObjectPass some extra settings to the default converter mentioned above. It supports the main options ofdate-fns, namingly:

includeSeconds -boolean - distances less than a minute are more detailed
addSuffix -boolean - results specifies if now is earlier or later than the date passed
useStrict -false -iftrue you need to use thestrictConverterOptions (see below)
strictConverterOptionsObjectPass some extra settings to the default converter mentioned above. It supports the main options ofdate-fns strict converter, namingly:

useStrict -true - needs to betrue, otherwise thedefaultConverterOptions have to be used (see above)
addSuffix -boolean - results specifies if now is earlier or later than the date passed
unit -second, minute, hour, day, month, year if specified, will force a unit
roundingMethod -floor, ceil, round which way to round partial units (default=round)

Once the plugin is registered you can straight up use it in your app.

Basic usage:

<template><timeago :datetime="date"/></template><script>...data(){return{date:'2021-09-01'}}...</script

Props

Instead of configurating the plugin during the registration, you can also configuate the component viaprops.

propertytyperequireddefaultdescription
datetimestring | Date | number✔️Thedatetime used to calculate the "time ago"
autoUpdatenumber | booleanfalseThe period of time to update the component,in seconds.
This can be omitted by setting it to0 orfalse.

The default value fortrue is60(seconds). Instead of passingtrue you can also pass a custom time.
localeLocale (seedate-fns/Locale)enThelocale specifies the language which is used to render the date. All availabledate-fns locales are supported by default.
converterdate, defaultConverterOptions | strictConverterOptions) => stringSee plugin options above
defaultConverterOptionsObjectSee plugin options above
strictConverterOptionsObjectSee plugin options above

[8]ページ先頭

©2009-2025 Movatter.jp