Giter Club home page Giter Club logo

chronometric's Introduction

chronometric

codecov GitHub Workflow Status (master) GitHub Workflow Status (develop) npm npm

JavaScript library for working with time durations in "1mo 1w 1d" format.

Install

$ npm install --save chronometric

CodeSandbox

You can find CodeSandbox template here

Basic usage

Recommended

import { Chronometric } from 'chronometric';

const chronoA = new Chronometric(2200);
const chronoB = Chronometric.fromString('1w 1d');
const chronoC = new Chronometric({ d: 1, h: 1 });

Via Node.js require()

const { Chronometric } = require('chronometric');

const chronoA = new Chronometric(2200);
const chronoB = Chronometric.fromString('1w 1d');
const chronoC = new Chronometric({ d: 1, h: 1 });

Via UNPKG

<script src="https://unpkg.com/chronometric@latest"></script>
<script>
  const { Chronometric } = window.chronometric;

  const chronoA = new Chronometric(2200);
  const chronoB = Chronometric.fromString('1w 1d');
  const chronoC = new Chronometric({ d: 1, h: 1 });
</script>

Features

Custom global and instance unit configuration

import { Chronometric } from 'chronometric';

// global configuration
Chronometric.defaultConversionRatios = {
  ms: 1,
  d: 24 * 60 * 60 * 1000
};
console.log(Chronometric.fromString("1d") + 0); // will output 86400000

// instance configuration
const chronoB = new Chrono(
  2200,
  {
    ms: 1,
    s: 1000
  }
);
console.log(chronoB.toString()); // will output "2s 200ms"

Works with JavaScript Date objects

import { Chronometric } from 'chronometric';

const dateFrom = new Date();
const dateTo = new Date(0);

const chrono = new Chronometric(dateFrom - dateTo); // will contain timespan between dateFrom and dateTo
import { Chronometric } from 'chronometric';

const now = Date.now();
const tomorrow = new Date(now + Chronometric.fromString("1d"));
const inAWeek = new Date(now + Chronometric.fromString("1w"));

Worktime units

import {
  Chronometric,
  HOUR_TO_MS_CONVERSION_RATIO,
  MINUTE_TO_MS_CONVERSION_RATIO
} from 'chronometric';

Chronometric.defaultConversionRatios = {
	ms: 1,
  m: MINUTE_TO_MS_CONVERSION_RATIO,
  h: HOUR_TO_MS_CONVERSION_RATIO,
  d: 8 * HOUR_TO_MS_CONVERSION_RATIO, // 8 hour work day
  w: 5 * 8 * HOUR_TO_MS_CONVERSION_RATIO // 5 day work week
};

const spentTime = ["9h 30m", "12h 22m"];
const totalSpentTime = new Chronometric(
  spentTime
  	.reduce((acc, item) => acc + Chronometric.fromString(item), 0)
).toString(); // "2d 5h 52m"

Known issues

  • When using big and small conversion ratios simultaniously (i.e. 1 year and 1 nanosecond to milliseconds) small ones can be lost due to JavaScript number type precision (see Number.MAX_SAFE_INTEGER)

chronometric's People

Contributors

yukipastelcat avatar dependabot[bot] avatar

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.