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

timean

v1.0.4

Published

A lightweight (~1.2kB gzipped) library to format date strings based on time difference.

Downloads

2

Readme

Timean.js

A lightweight (~1.2kB gzipped) library to format date strings based on time difference.

Features

  • 🕊️ Ligthweight
  • 💨 Blazing fast
  • 📦 Browser, Node.js and React Native compatible
  • 🧹 Clean and intuitive API
  • 📖 Good documentation and support

Timean.js allows you to format meaningful date strings like:

just now
5 days ago
1 year ago
10 seconds ago
in 2 days
in 3 months
and more...

Table of contents

Installation

yarn add timean

or

npm install --save timean

How to use?

To start to use this package you need to import it:

import timean from "timean"; //ES6 import

or

const timean = require("timean"); //CommonJS

Now you can call use the appropriated method:

const date = new Date('2021-01-01');
const formattedString = timean.fromNow(date);
const date1 = new Date('2021-01-01');
const date2 = new Date('2021-01-02');
const formattedString = timean.from(date1, date2);

Customizing (Locale)

You can pass the an object config as parameter when call from() or fromNow(), like this:

//...
// See the config options below on this docs.
const customConfig = {
  current: 'agora',
  past: {
    day: { singular: '%d dia atrás', plural: '%d dias atrás' },
    minute: { singular: '%d minuto atrás', plural: '%d minutos atrás' },
    hour: { singular: '%d hora atrás', plural: '%d horas atrás' },
    month: { singular: '%d mês atrás', plural: '%d meses atrás' },
    second: "%d segundos atrás", //You can use a string directly.
    year: { singular: '%d ano atrás', plural: '%d anos atrás' }
  },
}
const formattedString = timean.from(date1, date2, customConfig);

If you want to define a default configuration and avoid to pass a config parameter on each call of from(...) or fromNow(...), you can create a instance of Timean using:

//...
const timeFormatter = timean.create({ ...config });
const formattedString = timeFormatter.from(date1, date2);

Note: all fields are optional, when you overwrite a field just it will be merged with default configuration.

Customizing (Replacer)

By default, Timean.js replaces "%d" by the calculated time difference result. You can edit the replacer combination passing a property replacer: any_string_or_regex_you_want in the config object.

API

Timean has a clean and powerful API, it allows you to generate formatted time strings easily.

create(config?: LocaleConfig)
from(initialDate: Date, finalDate: Date, config?: LocaleConfig)
fromNow(date: Date, config?: LocaleConfig)

Types

type StringWithPlural = { singular: string; plural: string; }
type LocaleConfig = {
  current: string,
  replacer: string,
  past: {
    day: string | StringWithPlural,
    hour: string | StringWithPlural,
    minute: string | StringWithPlural,
    month: string | StringWithPlural, 
    second: string | StringWithPlural,
    year: string | StringWithPlural,
  }, 
  future: {
    day: string | StringWithPlural,
    hour: string | StringWithPlural,
    minute: string | StringWithPlural,
    month: string | StringWithPlural, 
    second: string | StringWithPlural,
    year: string | StringWithPlural,
  }
}

Help me help you

I will be very happy if you can support me to keep creating new open source packages.

Consider ⭐ starring this repository and 📢 spread the word about this package!

"Buy Me A Coffee"

License

MIT