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

intimeago

v1.1.0

Published

Convert dom elements with static datetime info into real-time 'in time' / 'time ago' elements.

Downloads

1

Readme

Intimeago

In time / Time ago - with update events

Intimeago is a library to turn date elements such as <span data-datetime="2020-11-18T12:03:57+00:00"></span> into 'in 10 minutes', '25 seconds ago', 'just now'... type of text nodes and have them update in realtime.

Examples:

// Past //
just now
25 seconds ago
1 minute ago
2 hours ago
3 days ago
5 weeks ago
2 years ago

// Future //
in 15 seconds
in 2 minutes
in 18 days
in 4 months

Usage

  • Install
npm install intimeago
  • Import
import { setup, remove, format } from 'intimeago'

or import with script tag in html file and access it via global variable intimeago.

<script src="dist/index.min.js"></script>
  • Example
<div class="intimeago" data-intimeago-datetime="2016-06-30 09:20:00"></div>
// Setup dom elements with realtime changes
setup(document.querySelectorAll('[data-intimeago-datetime]')) 

// OR

// Format the datetime and return as static string
format('2020-11-18T12:03:57+00:00')
  • Advanced example
<div class="intimeago" 
     data-intimeago-datetime="2016-06-30 09:20:00"
     data-intimeago-relative-datetime="2010-01-05 13:10:00"
     data-intimeago-prepend-text="Starts "
     data-intimeago-remove-on-zero="true"></div>

CDN

Alternatively you can also use a CDN which will reflect the latest version.

<script src="//unpkg.com/intimeago"></script>

API

  • setup

setup(node[, locale = 'en_US', options])

Add realtime updates to a dom element with data-intimeago-datetime attribute.

HTML code:

<div class="intimeago" data-intimeago-datetime="2016-06-30 09:20:00"></div>

JS code:

import { setup } from 'intimeago'

const nodes = document.querySelectorAll('.intimeago')

// use setup method to add realtime updates to nodes
setup(nodes)

// OR

// set locale and relative date to calculate the difference from
setup(nodes, 'en_US', { relativeDateTime: '2016-10-04 13:30:00' })

// subscribe to an event that happens on update (change of text of the element)
node.addEventListener('intimeago-update', (e) => {
 // e.detail.diff // difference between the specified date and now/relativeDateTime (in seconds)
})

Options: - relativeDateTime allows you to set the date and time to calculate the difference from. Default is NOW.

  • remove

remove(node)

Remove the realtime updates from a dom element.

JS code:

import { remove } from 'intimeago'

const node = document.querySelector('.intimeago')

// remove realtime updates from node
remove(node)
  • format

format(date[, locale = 'en_US', opts])

Format a Date instance / timestamp / date string to static string.

import { format } from 'intimeago'

// format timestamp
format(1605702147658)

// format date instance
format(new Date(1605702147658))

// format ISO date string
format('2020-11-18T12:23:28.410Z')

// format with locale
format(1605702147658, 'en_US')

// format with locale and relative date
format(1605702147658, 'en_US', { relativeDateTime: '2010-10-04 10:45:00' })

The default locale is en_US.

list of all locales here.

  • importLocale

Add new locale function to be used

import { importLocale } from 'intimeago'

// create the translation function
const myCoolLocaleFunction = function (number, index) {
    return [
     // [past tense , future tense] 
     ['just now', 'in %s seconds'], // 0-10 seconds
     ['%s seconds ago', 'in %s seconds'], // 10 - 60 seconds
     ['1 minute ago', 'in 1 minute'], // 1-2 minutes
     ['%s minutes ago', 'in %s minutes'], // 2 minutes - 1 hour
     ['1 hour ago', 'in 1 hour'], // 
     ['%s hours ago', 'in %s hours'],
     ['1 day ago', 'in 1 day'],
     ['%s days ago', 'in %s days'],
     ['1 week ago', 'in 1 week'],
     ['%s weeks ago', 'in %s weeks'],
     ['1 month ago', 'in 1 month'],
     ['%s months ago', 'in %s months'],
     ['1 year ago', 'in 1 year'],
     ['%s years ago', 'in %s years'],
    ][index]
}

// import it
importLocale('mycool_locale', myCoolLocaleFunction )

// use it
format(new Date(1605702147658), 'mycool_locale')
  • Tag Attributes

The following attributes will automatically be used if present on intimeagoified html element:

data-intimeago-datetime - datetime sting to calculate the difference from

data-intimeago-relative-datetime - datetime sting to calculate the difference relative to. Default is NOW.

data-intimeago-prepend-text - text to prepend before the difference text ('Starts in 2 minutes')

data-intimeago-remove-on-zero - remove the element from dom when difference is 0

<div class="intimeago"
     data-intimeago-datetime="2016-06-30 09:20:00"
     data-intimeago-relative-datetime="2010-01-05 13:10:00"
     data-intimeago-prepend-text="Starts "
     data-intimeago-remove-on-zero="true"></div>

NOTE

This repo has been heavily inspired by hustcc/timeago.js. All the credits go to all involved.

LICENSE

MIT@DavidHavl