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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@date-js/dom-timeago

v3.0.0

Published

Lightweight relative-time library for the DOM — live auto-refresh with smart per-rule intervals and full customization.

Downloads

72

Readme

@date-js/dom-timeago

npm size stars

One attribute. Live, human-readable dates. Zero boilerplate.

<span data-timeago="1579950627">26/01/2020</span>
<!-- becomes → "2 minutes ago", then "Today at 12:10", then "Yesterday at 12:10"… automatically -->

Live demo


Why @date-js/dom-timeago?

Most relative-date libraries give you a formatting function. You call it once, you get a string, and you're on your own: looping over elements, setting up timers, deciding when to re-render.

@date-js/dom-timeago works differently. Put a timestamp in an attribute, call DomTimeago.start() once, and every date on the page stays current on its own.

What sets it apart:

  • HTML-first: no JS per element. One attribute, one start() call, done
  • Smart per-item scheduling: each rule sets its own refresh interval. "30 seconds ago" re-evaluates on the next tick; "March 12" never re-evaluates. Items that no longer need updates are dropped from tracking entirely
  • Rule-based rendering: the display logic is a plain list of conditions you fully control. Override a default, add your own, or replace them all
  • Auto-discovery: dynamically added elements are picked up automatically via MutationObserver, works seamlessly in SPAs
  • Tiny: ~3 KB gzipped
  • Multilingual: extensible to any locale by adding a key in each rule's text

Install & Usage

npm / bundler

npm install @date-js/dom-timeago
import DomTimeago from '@date-js/dom-timeago';
DomTimeago.start();

<script> tag, UMD (CDN)

No build step. DomTimeago is available as a global.

<script src="https://cdn.jsdelivr.net/npm/@date-js/dom-timeago@3/dist/dom-timeago.umd.js"></script>
<script>
  DomTimeago.start();
</script>

<script type="module">, ESM (CDN)

<script type="module">
  import DomTimeago from 'https://cdn.jsdelivr.net/npm/@date-js/dom-timeago@3/dist/dom-timeago.esm.js';
  DomTimeago.start();
</script>

In all cases, the original content is preserved as a title attribute, and the element updates live:

<span title="26/01/2020 12h10">2 minutes ago</span>

Stop it anytime:

DomTimeago.stop();

Configuration

DomTimeago.start({
  refresh: 5,            // global re-render interval in seconds (default: 5)
  selector: 'data-timeago', // attribute to look for (default: 'data-timeago')
  rules: [],             // custom rules, evaluated before defaults
});

Custom rules

Rules are evaluated in order; the first matching one wins. Each rule defines when it applies, what to display, and how often to refresh.

DomTimeago.start({
// or DomTimeago.start({
  rules: [
    {
      condition: (interval) => interval.day >= 365 * 10,
      refresh: null, // never re-renders
      text: {
        en: "%dd day{%dd||s} ago (year %Y)",
        fr: "il y a %dd jour{%dd||s} (année %Y)",
      }
    }
  ]
});

Custom rules are evaluated before the defaults; the first matching rule wins.

See all default rules in src/defaultRules.ts and the DateInterval shape in src/DateInterval/DateInterval.ts.

Rule properties

| Property | Type | Description | |---|---|---| | condition | (interval: DateInterval) => boolean | Returns true if this rule applies. interval contains day, hour, minute, second (elapsed) and date (original Date) | | text | Record<string, string> | Locale-keyed display templates. A rule is skipped if the current locale key is missing | | refresh | number \| null \| undefined | Seconds until next render (null = never, undefined = global interval) |

Template tokens

Interval tokens (elapsed time): %ds seconds · %dm minutes · %dh hours · %dd days

Date tokens (formatted via @date-js/date-formatter): %H hour · %i minutes · %d day · %j day (no leading zero) · %F month name · %Y year · %l weekday

Pluralization: {%dd|singular|plural}

Languages

Detected automatically from navigator.language.

To add a language, include a key in each rule's text object matching the locale code (e.g. "de", "es").