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

@nteract/timeago

v3.6.5

Published

A simple Time-Ago component for React

Downloads

33

Readme

TimeAgo

Usage:

@nteract/timeago is a very simple component that takes a date prop and returns a span with live updating date in a time-ago format. The date will update only as often as needed. For timestamps below a minute away — every second, for timestamps up to 5 minutes away — every hour, and so on.

timeago does the minimum amount of updates necessary.

<TimeAgo date="Aug 29, 2014" />

Language support

@nteract/timeago comes with support for a large number of languages out of the box. This support is based on the string objects taken from jquery-timeago

Usage:

To use any of the languages provided, other than the default english, you will have to import the language strings and build a custom formatter.

import TimeAgo from '@nteract/timeago'
import frenchStrings from '@nteract/timeago/lib/language-strings/fr'
import buildFormatter from '@nteract/timeago/lib/formatters/buildFormatter'

const formatter = buildFormatter(frenchStrings)

// in your react component
<TimeAgo date='Feb 1, 1966' formatter={formatter} />

And that's it. You can also customize the language strings or provide your own. For maximum control you can write your own formatter function.

For best performance, I recommend that you build formatters that you're going to use once, and pass them around.

Props

date (required)

Date is a date in the past or the future. This can be a Date Object, A UTC date-string or number of milliseconds since epoch time.

now (optional)

A function that returns what Date.now returns. Useful for server-side rendering.

live (optional)

@nteract/timeago is live by default and will auto update its value. However, if you don't want this behaviour, you can set live: false.

formatter (optional)

A function that takes five arguments:

  • value: An integer value, already rounded off
  • unit: A string representing the unit in english. This could be one of:
    • 'second'
    • 'minute'
    • 'hour'
    • 'day'
    • 'week'
    • 'month'
    • 'year'
  • suffix: A string. This can be one of
    • 'ago'
    • 'from now'
  • date: The actual date you are trying to represent. Use this for a more custom format for showing your date.
  • defaultFormatter: (Optional) A default formatter function with pre-bound values. May be used as a fallback formatting option inside a custom formatting logic.

Here are some examples of what the formatter function will receive:

  • '5 minutes ago' => formatter(5, 'minute', 'ago')
  • '1 year from now' => formatter(1, 'year', 'from now')

The formatter function is a simple way to extend the functionality of React-Timeago to support any feature you may need from a fuzzy time display. The formatter function is expected to return a string. But it can also return any React component (or array of components) that would become the child of @nteract/timeago

The project comes with a set of languages and a formatter function builder based on those language strings. You can customize the strings, or provide your own custom formatter function.

I recommend using the fantastic L10ns for other internationalization needs.

component (optional) (default: 'time')

A string of ReactClass that is used to wrap the live updating string

title (optional)

If the component is left as the default 'time' component, a title attribute is passed to it. You can customize this by passing a string, or a UTC date-string will be used based on the given date.

minPeriod (optional) (default: 0)

The minimum number of seconds that the component should wait before updating. The component will still update if you pass new props. Use this if, for example, you don't want to update every second for recent times.

maxPeriod (optional) (default: Infinity)

The opposite of minPeriod. Use this to force dates to update more often than the default behaviour. For example, you can use this update a time every 5 minutes even after it is more than an hour old.

Anything Else? (optional)

As of v2.0 you can pass in any props. Any props not used by @nteract/timeago will be passed down to the resulting component. This means that you can pass className, styles, id, title, aria-label, event handlers or anything else you want.