Giter Site home page Giter Site logo

duration.js's Introduction

duration.js

Documentation

Check out Neko Of The Abyss for documentation.

Duration follows the performance.now() format, so microseconds and nanoseconds go after the decimal point.

Installation

$ npm install --save @retraigo/duration.js # NPM
$ pnpm install @retraigo/duration.js # PNPM

Usage

Basic Usage

// Node
import Duration from "@retraigo/duration.js"; 
// Deno
import Duration from "https://deno.land/x/[email protected]/mod.ts"; 

const Duration = await import("@retraigo/duration.js"); // Node with CommonJS

new Duration(); // Get duration since midnight

new Duration(3545346); // A random duration

new Duration(0); // Just 0

new Duration(-1); // Negative duration returns 0 too

Duration since a timestamp

const start = performance.now()
// Do some long task
const d = Duration.since(start)

Duration between two timestamps

const start = performance.now()
// Do some long task
const check = performance.now()
const d = Duration.between(start, check)

From Text

Duration.fromString("1m2s"); // Duration {d:0, h:0, m:1, s:2, ms:0}

Duration.fromString("4090 sec 4939  days 7342  hour 2324milliseconds 4344 min"); // // Duration {d: 5246, h: 13, m: 52, s: 12, ms: 324 }

You can also get the entire Duration in milliseconds through the raw property.

const dur = Duration.fromString(
  "4090 sec 4939  days 7342  hour 2324milliseconds 4344 min"
);
dur.raw; // 453304332324

Properties

Duration {
    raw: 0 // Original milliseconds passed to the constructor
    d: 0, // Days
    h: 0, // Hours
    m: 0, // Minutes
    s: 0, // Seconds
    ms: 0 // Milliseconds
    us: 0 // Microseconds
    ns: 0 // Nanoseconds
};

Formatting

const duration = new Duration(59834344.334)

console.log(duration)
// Duration { raw: 59834344.334, d: 0, h: 16, m: 37, s: 14, ms: 344, us: 334, ns: 0 }
console.log(duration.toDescriptiveString())
// 0 days, 16 hours, 37 minutes, 14 seconds, 344 milliseconds, 334 microseconds, 0 nanoseconds
console.log(duration.toShortString())
// 0d 16h 37m 14s 344ms 334us 0ns
console.log(duration.toWordString())
// zero days, sixteen hours, thirty seven minutes, fourteen seconds, 
// three hundred and forty four milliseconds, three hundred and thirty 
// four microseconds, zero nanoseconds
console.log(duration.toTimeString())
// 00:16:37:14:344:334:000

Changes from V3

  • Duration.prototype.stringify has been removed in favor of:
    • Duration.prototype.toDescriptiveString
    • Duration.prototype.toWordString
    • Duration.prototype.toShortString
    • Duration.prototype.toTimeString
  • addZero has been removed. Use the built-in String.prototype.padStart(n, "0") instead.
  • matchReg has been renamed to matchUnit.

Support

Join our Discord server here

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.