npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@retraigo/duration.js

v4.1.1

Published

Get the formatted time duration

Downloads

5

Readme

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