npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@marcreichel/alpine-timeago

v1.5.2

Published

Display the time difference between now and a given date in a human readable format

Downloads

5,189

Readme

🚀 Installation

CDN

Include the following <script> tag in the <head> of your document, just before Alpine.

<script src="https://cdn.jsdelivr.net/npm/@marcreichel/alpine-timeago@latest/dist/alpine-timeago.min.js" defer></script>

NPM

npm install @marcreichel/alpine-timeago

Add the x-timeago directive to your project by importing the package before starting Alpine.

import Alpine from 'alpinejs';
import TimeAgo from '@marcreichel/alpine-timeago';

Alpine.plugin(TimeAgo);

Alpine.start();

🪄 Usage

Directive

To convert a Date to the human-readable distance from now, add the x-data and x-timeago directives to an element and pass the date (as a Date or a string in ISO format) to the x-timeago directive. The directive will update the output every 30 seconds.

<span x-data="{ date: new Date() }" x-timeago="date"></span>

Under the hood the directive is using formatDistanceToNow from date-fns.

No suffix

If you do not want the "[diff] ago" suffix or "in [diff]" prefix, you can use the x-timeago.pure modifier.

<span x-data="{ date: new Date() }" x-timeago.pure="date"></span>

Include seconds

Distances less than a minute are more detailed.

<span x-data="{ date: new Date() }" x-timeago.seconds="date"></span>

Strict

If you do not want the "about [diff]" or "less than [diff]" prefixes, you can use the x-timeago.strict modifier.

<span x-data="{ date: new Date() }" x-timeago.strict="date"></span>

Strict options

The strict mode comes with its own set of modifiers for controlling the output.

Force unit

Use the x-timeago.strict.unit.[unit] modifier with one of the following units to force the unit in the output.

  • second (e.g. x-timeago.strict.unit.second)
  • minute (e.g. x-timeago.strict.unit.minute)
  • hour (e.g. x-timeago.strict.unit.hour)
  • day (e.g. x-timeago.strict.unit.day)
  • month (e.g. x-timeago.strict.unit.month)
  • year (e.g. x-timeago.strict.unit.year)
Adjust rounding method

By default, the values are rounded using the Math.round algorithm. If you would like to adjust this, use the x-timeago.rounding.[method] modifier with one of the following methods:

  • floor (e.g. x-timeago.strict.rounding.floor)
  • ceil (e.g. x-timeago.strict.rounding.ceil)
  • floor (e.g. x-timeago.strict.rounding.round)

Magic function

As of version 1.3.0 of this package a $timeago magic function is included which will return the human-readable distance from now.

<span x-data="{ date: new Date() }" x-text="$timeago(date)"></span>

Note: Using the magic function the distance does not get updated automatically. You have to update it yourself if you want to.

No suffix

If you do not want the "[diff] ago" suffix or "in [diff]" prefix, you can provide true as the second parameter to the function.

<span x-data="{ date: new Date() }" x-text="$timeago(date, true)"></span>

Include seconds

If you want distances less than a minute to be more detailed, you can provide true as the third parameter to the function.

<span x-data="{ date: new Date() }" x-text="$timeago(date, null, true)"></span>

Strict

If you do not want the "about [diff]" or "less than [diff]" prefixes, you can use the fourth parameter to provide the " strict" options.

Default
<span x-data="{ date: new Date() }"
      x-text="$timeago(date, null, null, { strict: true })"></span>
Unit
<span x-data="{ date: new Date() }"
      x-text="$timeago(date, null, null, { strict: true, unit: 'second' })"></span>
Rounding method
<span x-data="{ date: new Date() }"
      x-text="$timeago(date, null, null, { strict: true, roundingMethod: 'floor' })"></span>

Other locales

If you are using the npm installation method for this package or the ESM distribution, you can use the TimeAgo.configure() method to provide a different locale from date-fns.

import TimeAgo from '@marcreichel/alpine-timeago';
import {de} from 'date-fns/locale';

Alpine.plugin(TimeAgo.configure({
    locale: de,
}));

📄 License

Copyright (c) 2022 Marc Reichel and contributors.

Licensed under the MIT license, see LICENSE for details.