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

tiempo

v1.1.5

Published

Library to format time and dates

Downloads

15

Readme

Tiempo - GitHub license npm version

Library to format time into text with a custom format.

Install

yarn add tiempo
# or
npm install tiempo

Format time

import tiempo from 'tiempo';

// The format function if the second parameter is not defined, it will use current date as now.

// now: 2018-07-01 14:30:50.
tiempo.format('2018-07-01 14:30:00', '2018-07-01 14:30:50');
// returns: 50 seconds ago.
tiempo.format('2018-07-01 14:15:00', '2018-07-01 14:30:50');
// returns: 15 minutes ago.
tiempo.format('2018-06-30 16:10:00', '2018-07-01 14:30:50');
// returns: yesterday at 16:10.

Change locale

import tiempo from 'tiempo';

// same example as above.

// if locale doest exist it will warn you to register new locale.
tiempo.config({ locale: 'es' });

// now: 2018-07-01 14:30:50.
tiempo.format('2018-07-01 14:30:00', '2018-07-01 14:30:50');
// returns: hace 50 segundos.
tiempo.format('2018-07-01 14:15:00', '2018-07-01 14:30:50');
// returns: hace 15 minutos.
tiempo.format('2018-06-30 16:10:00', '2018-07-01 14:30:50');
// returns: ayer a las 16:10.

Add a custom or new locale

import tiempo from 'tiempo';

// register locale, if it exist will be replaced otherwise added as a new one.
config({
  locales: {
    es: {
      relativeTime: {
        // [PAST,FUTURE]
        s: ['recien', 'ahora'],
        ss: ['hace %s segundos', 'en %s segundos'],
        m: ['hace un minuto', 'en un minuto'],
        mm: ['hace %m minutos', 'en %m minutos'],
        h: ['hace una hora', 'en una hora'],
        hh: ['hoy a las {hh:mm}'],
        d: ['ayer a las {hh:mm}', 'mañana a las {hh:mm}'],
        dd: ['hace %d días', 'en %d dias'],
        M: ['hace un mes', 'en un mes'],
        MM: ['hace %M meses', 'en %M meses'],
        y: ['hace un año', 'en un año'],
        yy: ['hace %y años', 'en %y años'],
      },
    },
  },
});

Replace relative time

import tiempo from 'tiempo';

// This will replace all locales(*) or a specific one, only is replaced de relativeTime you want to replace, if you replace 'dd' for a new one, it will replace that one and the rest will remain the same
config({
  locales: {
    '*': {
      relativeTime: {
        // [PAST,FUTURE]
        MM: ['{dd/MM/yy hh:mm}'],
        yy: ['{dd/MM/yyyy hh:mm A}'],
      },
    },
    en: {
      relativeTime: {
        // [PAST,FUTURE]
        dd: [
          '%d days ago at {hh:mm A}',
          'in %d days %h hours %m minutes %s seconds',
        ],
        MM: ['{dd/MM/yy hh:mm}'],
        yy: ['{dd/MM/yyyy hh:mm A}', 'in %y years'],
      },
    },
  },
});

Get difference between two dates

import tiempo from 'tiempo';

tiempo.diff('2018-01-04 22:00:00', '2018-01-02 23:00:00');
// returns: { y: 0, M: 0, d: 1, h: 23, m: 0, s: 0 }

Realtime format

import tiempo from 'tiempo';

// Set realtime in config
tiempo.config({ realtime: true });

// Use realtime format
tiempo
  .realtime(newFormat => console.log(newFormat))
  .format('2018-10-04 19:37:00');

Functionality

export interface Tiempo {
  format(d1: string | Date, d2?: string | Date): string;
  diff(d1: string | Date, d2: string | Date): DifferenceTime;
  config(options: Partial<ConfigOptions>): any;
  realtime(fn: (result?: string) => void): Realtime;
}

Config

import tiempo from 'tiempo';

tiempo.config({
  realtime: true, // Set realtime
  format: ['s', 'm', 'h', 'd'], // The formats you want to use and ignore the rest
  locale: 'es',
  locales: {es:{relativeTime:{...}}},
});

// All configs are merged into one, so you can change it multiple times without losing data(except format)
tiempo.config({
  format: ['s'], // The formats you want to use and ignore the rest
  locale: 'en',
  locales: {en:{relativeTime:{...}}},
});

// This will result in

{
  realtime: true,
  format: ['s'],
  locale: 'en',
  locales: {es:{relativeTime:{...}},en:{relativeTime:{...}}},
}

Format Table to use inside format {}.

example: '{hh:mm A} == 01:05 AM'

| | Token | Output | | -----: | :---: | :-------------------------------- | | Second | s | 0 1 ... 58 59 | | | ss | 00 01 ... 58 59 | | Minute | m | 0 1 ... 58 59 | | | mm | 00 01 ... 58 59 | | Hour | h | 1 2 ... 23 24 | AM/PM ...11 12 | | | hh | 01 02 ... 23 24 | AM/PM ...11 12 | | Day | d | 1 2 ... 11 12 | | | dd | 01 02 ... 11 12 | | Month | M | 1 2 ... 11 12 | | | MM | 01 02 ... 11 12 | | Year | y | 1 2 ... 11 12 | | | yy | 01 02 ... 11 12 | | AM/PM | A | AM PM | | | a | am pm |

Licence

MIT