1.1.5 • Published 6 years ago

tiempo v1.1.5

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

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'

TokenOutput
Seconds0 1 ... 58 59
ss00 01 ... 58 59
Minutem0 1 ... 58 59
mm00 01 ... 58 59
Hourh1 2 ... 23 24 | AM/PM ...11 12
hh01 02 ... 23 24 | AM/PM ...11 12
Dayd1 2 ... 11 12
dd01 02 ... 11 12
MonthM1 2 ... 11 12
MM01 02 ... 11 12
Yeary1 2 ... 11 12
yy01 02 ... 11 12
AM/PMAAM PM
aam pm

Licence

MIT

1.1.5

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.7.4

6 years ago

0.7.3

6 years ago

0.6.3

6 years ago

0.6.1

6 years ago

0.5.3

6 years ago

0.5.2

6 years ago

0.5.1

6 years ago

0.5.0

6 years ago

0.1.9

6 years ago

0.1.8

6 years ago

0.1.7

6 years ago

0.1.6

6 years ago

0.1.5

6 years ago