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

@date-js/clever-date

v2.2.0

Published

A Javascript module to show an intelligent date refreshed at regular intervals.

Downloads

3

Readme

Clever Date

npm npm starts

A javascript library (<10kB) to show an intelligent date refreshed at regular intervals.

Demo

A demo is available here.

Supported languages

Languages defined bellow are fully supported but you can add your own rules with other languages.

  • English
  • Français

You can also contribute and suggest translations with a pull request.

Example

Add an attribute to your date with the corresponding timestamp.

<div data-clever-date="1579950627">26/01/2020 12h12</div>
<div data-clever-date="1580037027">25/01/2020 12h12</div>

Start the script:

CleverDate.start();

Let's see the result:

<div title="26/01/2020 12h12">2 minutes ago</div>
<div title="25/01/2020 12h12">Yesterday at 12:12</div>

Some possible results:

  • Just now / A l'instant
  • 2 minutes ago / Il y a 2 minutes
  • 2 hours ago / Il y a 2 heures
  • Today at 11:46 / Aujourd'hui à 11h46
  • Yesterday at 11:46 / Hier à 11h46

Install

ES6

npm install @date-js/clever-date
import CleverDate from '@date-js/clever-date';

Otherwise

<script src="https://cdn.jsdelivr.net/npm/@date-js/[email protected]"></script>

Usage

Start the process :

CleverDate.start();

Stop the process:

CleverDate.stop();

If you add element dynamically.

window.dispatch(new Event('clever-date.update'));

Customize it

You have the full possibility to customize your rules by passing your configuration.

var configuration = {
    refresh: 5, // The minimal refreshing time
    selector: 'data-clever-date', // Elements with this attribute will be parsed
    rules: [
        {
             condition: function(dateIntervalItem) { return dateIntervalItem.day >= 365*10; }, text: {
                 fr: "Il y a %dd jour{%dd||s} (année %Y)",
                 en: "%dd day{%dd||s} ago (year %Y)"
             }
        }
    ]
}

CleverDate.start(configuration);

How create rules ?

You can see examples in default rules file.

A rule is composed of some elements:

condition

A callback which returns true if the rule matches.
DateInterval is injected in the callback: see DateInterval.ts for more information.

refresh

For improving performances, it's not necessary to analyse and parse your rule each time.

  • null: never analysed again
  • undefined: use default refreshing time
  • number: seconds between analyses
text

An object with the text for each language that you want to target.