Duration
A small and fast time utility which makes working with time, duration, date super easy. It contains a number of useful small utility methods to work with time and date.
Documentation
Read Full Documentation here.
Install
npm i @imranbarbhuiya/duration
yarn add @imranbarbhuiya/duration
Usage
esm or Typescript
import { parse, format, prettyFormat, date, relativeTime, Formatter } from '@imranbarbhuiya/duration';
CommonJS
const { parse, format, prettyFormat, date, relativeTime, Formatter } = require('@imranbarbhuiya/duration');
parse
parse('1s 1min 1h 1d 1w 1mo 1y');
parse('2 days');
parse('1d');
parse('-3 days');
format
format(2 * 60000);
format(-3 * 60000, { long: true });
format(parse('10 hours'), { long: true });
prettyFormat
prettyFormat(60000);
prettyFormat(121000);
prettyFormat(1000 * 60 * 60 * 60 * 24 * 30, { format: 'short' });
prettyFormat(86406010, {
format: 'short',
patterns: ['hour', 'minute', 'second', 'millisecond'],
separator: ' '
});
Writing these patterns every time is a pain. That's why there is a Formatter class which can be used to format multiple times without repeating yourself.
Formatter
const formatter = new Formatter({
format: 'short',
patterns: ['hour', 'minute', 'second', 'millisecond'],
separator: ' '
});
formatter.format(1000 * 60 * 60 * 24 * 30);
formatter.format(86406010);
date
date('2022-01-01', 'YYYY-MMM-Do');
date('2022-01-01T00:00:00.000Z', 'yyyy-MM-D HH:mm:ss.SS Z');
date('2022-01-01T00:00:00.000Z', 'yyyy-MMMM-DDD HH:mm:ss.SS');
relativeTime
relativeTime(Date.now() + 1000);
relativeTime(Date.now() - 1000 * 60 * 60);
Buy me some doughnuts
If you want to support me by donating, you can do so by using any of the following methods. Thank you very much in advance!
Contributors ✨
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!