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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@rbardini/time-duration

v1.0.0

Published

A web component to format `<time>` elements with the elapsed time

Downloads

8

Readme

@rbardini/time-duration

npm package version Build status

⏱️ A web component to format <time> elements with the elapsed time.

[!IMPORTANT] This component depends on APIs with limited availability across major browsers as of Oct 2024. See polyfilling for more information.

<time-duration>
  <time datetime="2020-01-01">
    <!-- 👇 will be replaced with current duration, e.g. "4 yrs, 6 mths" -->
    Jan 1, 2020 – Present
  </time>
</time-duration>

View demos →

Installation

Via package manager

npm install @rbardini/time-duration
import '@rbardini/time-duration/register'

Via <script> tag

Download the latest release into your project and:

<!-- host yourself -->
<script type="module" src="register.js"></script>

or embed from a third-party CDN (update version as needed; not recommended for production use):

<!-- esm.sh CDN -->
<script type="module" src="https://esm.sh/@rbardini/[email protected]/register"></script>
<!-- UNPKG CDN -->
<script type="module" src="https://www.unpkg.com/@rbardini/[email protected]/register.js"></script>

Usage

<time-duration> must wrap a <time> element with either a datetime attribute or a valid datetime text content:

<time-duration>
  <time datetime="2020-01-01">Jan 1, 2020 – Present</time>
</time-duration>

<!-- text content is used if no `datetime` attribute is defined -->
<time-duration>
  <time>2020-01-01</time>
</time-duration>

[!NOTE] Durations are formatted once and not updated in real time.

The datetime attribute will also be set to a duration string representing the elapsed time, if not one already.

Options

| Attribute | Description | Example | Default | | ------------------- | ----------------------------------------------------- | -------------- | --------------------- | | data-ref-datetime | The reference date and time of the resulting duration | "2030-01-01" | Current date and time |

[!NOTE] Durations are never assumed to be negative—if the reference date and time is earlier than the target date and time, the duration is calculated from the reference to the target, otherwise from the target to the reference.

Data attributes are also forwarded as options to datetime/duration method calls. For example, to set the duration's largestUnit and smallestUnit, as well as the formatting style, simply declare them as data attributes:

<time-duration data-largest-unit="year" data-smallest-unit="month" data-style="long">
  <time datetime="2020-01-01">Years since Jan 1, 2020</time>
</time-duration>

See datetime.until() and duration.toLocaleString() for more options.

Polyfilling

<time-duration> relies on the Temporal and Intl.DurationFormat APIs to work, which are not widely available yet. You may want to wait until browser support improves before using this component.

If you want to use it today, however, you must polyfill these APIs. You can include the temporal-polyfill and @formatjs/intl-durationformat polyfills in your project, for example.

Optionally, <time-duration> can load these polyfills from esm.sh for you via the polyfill+register entry point (not recommended for production use):

import '@rbardini/time-duration/polyfill+register'

or

<script type="module" src="polyfill+register.js"></script>