GithubHelp home page GithubHelp logo

imranbarbhuiya / duration Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 3.0 12.25 MB

A small and fast utility to convert milliseconds to human-readable string or vice versa.

Home Page: https://duration.js.org

License: MIT License

TypeScript 100.00%
date duration ms parse time formatter hacktoberfest

duration's Introduction

Duration

npm codecov npm

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
npm i @imranbarbhuiya/duration

# yarn
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

// string => number
parse('1s 1min 1h 1d 1w 1mo 1y'); // 34844461001
parse('2 days'); //=> 172800000
parse('1d'); //=> 86400000
parse('-3 days'); //=> -259200000

format

// number => string
format(2 * 60000); //=> 2m
format(-3 * 60000, { long: true }); //=> -3 minutes
format(parse('10 hours'), { long: true }); //=> 10 hours

prettyFormat

// number to string (multiple units)
prettyFormat(60000); //=> 1 minute
prettyFormat(121000); //=> 2 minutes, 1 second
prettyFormat(1000 * 60 * 60 * 60 * 24 * 30, { format: 'short' }); //=> 1mo
prettyFormat(86406010, {
	format: 'short',
	patterns: ['hour', 'minute', 'second', 'millisecond'],
	separator: ' '
}); //=> 24h 6s 10ms

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); //=> 720h
formatter.format(86406010); //=> 24h 6s 10ms

date

// format a date
date('2022-01-01', 'YYYY-MMM-Do'); //=> 2022-Jan-Saturday
date('2022-01-01T00:00:00.000Z', 'yyyy-MM-D HH:mm:ss.SS Z'); //=> 2022-01-1 00:00:00.00 0
date('2022-01-01T00:00:00.000Z', 'yyyy-MMMM-DDD HH:mm:ss.SS'); //=> 2022-January-Sat 00:00:00.00

relativeTime

// format a date as relative date
relativeTime(Date.now() + 1000); //=> in a few seconds
relativeTime(Date.now() - 1000 * 60 * 60); //=> an hour ago

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!

Buy Me A Coffee Buy Me a Coffee at ko-fi.com

Contributors โœจ

Thanks goes to these wonderful people (emoji key):


Parbez

๐Ÿ’ป ๐Ÿšง ๐Ÿค”

WhiteSource Renovate

๐Ÿšง

This project follows the all-contributors specification. Contributions of any kind welcome!

duration's People

Contributors

dependabot[bot] avatar imranbarbhuiya avatar kikobeats avatar lukeed avatar renovate-bot avatar renovate[bot] avatar sarcasterxo avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

duration's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/build.yml
  • actions/checkout v4.1.6@a5ac7e51b41094c92402da3b24376905380afc29
  • actions/setup-node v4.0.2@60edb5dd545a775178f52524783378180af0d1f8
.github/workflows/codeql-analysis.yml
  • actions/checkout v4@a5ac7e51b41094c92402da3b24376905380afc29
  • github/codeql-action v3@2e230e8fe0ad3a14a340ad0815ddb96d599d2aff
  • github/codeql-action v3@2e230e8fe0ad3a14a340ad0815ddb96d599d2aff
  • github/codeql-action v3@2e230e8fe0ad3a14a340ad0815ddb96d599d2aff
.github/workflows/deploy.yml
  • actions/checkout v4.1.6@a5ac7e51b41094c92402da3b24376905380afc29
  • actions/setup-node v4.0.2@60edb5dd545a775178f52524783378180af0d1f8
  • JamesIves/github-pages-deploy-action v4.6.1@5c6e9e9f3672ce8fd37b9856193d2a537941e66c
.github/workflows/lint.yml
  • actions/checkout v4.1.6@a5ac7e51b41094c92402da3b24376905380afc29
  • actions/setup-node v4.0.2@60edb5dd545a775178f52524783378180af0d1f8
  • wearerequired/lint-action v2@548d8a7c4b04d3553d32ed5b6e91eb171e10e7bb
.github/workflows/release.yml
  • actions/checkout v4.1.6@a5ac7e51b41094c92402da3b24376905380afc29
  • actions/setup-node v4.0.2@60edb5dd545a775178f52524783378180af0d1f8
.github/workflows/test.yml
  • actions/checkout v4.1.6@a5ac7e51b41094c92402da3b24376905380afc29
  • actions/setup-node v4.0.2@60edb5dd545a775178f52524783378180af0d1f8
  • codecov/codecov-action v4.4.1@125fc84a9a348dbcf27191600683ec096ec9021c
npm
package.json
  • @favware/cliff-jumper ^3.0.3
  • @vitest/coverage-v8 ^1.6.0
  • eslint ^8.57.0
  • eslint-config-mahir ^0.0.43
  • prettier ^3.3.0
  • tsup ^8.1.0
  • typedoc ^0.25.13
  • typescript ^5.4.5
  • vitest ^1.6.0
  • yarn 4.2.2

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.