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

duration

v0.2.2

Published

Time duration utilities

Downloads

6,498,328

Readme

*nix build status Windows build status Transpilation status npm version

duration - Time duration utilities

Formerly part of es5-ext project.

Installation

Node.js

$ npm install duration

Browser

Can be bundled for browser with help of modules-webmake

Example usage:

var Duration = require("duration");

var duration = new Duration(new Date(2000, 6, 7), new Date(2010, 8, 13, 3, 23, 8, 456));

console.log("Years: ", duration.years);
console.log("Months: ", duration.months);
console.log("Days: ", duration.days);
console.log("Hours: ", duration.hours);
console.log("Minutes: ", duration.minutes);
console.log("Seconds: ", duration.seconds);
console.log("Milliseconds: ", duration.milliseconds);

console.log("Trailing months: ", duration.month);
console.log("Trailing days: ", duration.day);
console.log("Trailing hours: ", duration.hour);
console.log("Trailing minutes: ", duration.minute);
console.log("Trailing seconds: ", duration.second);
console.log("Trailing milliseconds: ", duration.millisecond);

console.log("Default string representation: ", duration.toString());
console.log("Alternative string representation: ", duration.toString(1));
console.log("Custom string representation: ", duration.toString("H: %Hs m: %M"));

Output:

Years:  10
Months:  122
Days:  3720
Hours:  89283
Minutes:  5357003
Seconds:  321420188
Milliseconds:  321420188456
Trailing months:  2
Trailing days:  6
Trailing hours:  3
Trailing minutes:  23
Trailing seconds:  8
Trailing milliseconds:  456
Default string representation:  10y 2m 6d 03:23:08.456
Alternative string representation:  10y 2m 6d 3h 23m 8s 456ms
Custom string representation:  H: 89283 m: 23

Duration(from[, to])

Main module is both constructor and factory method, and can be used either way.
from and to are expected to be JavaScript Date objects. to is optional, and if not provided it defaults to current time.

Duration.prototype properties

years

Returns full years of the duration

months

Returns full months of the duration

days

Returns full days of the duration

hours

Returns full hours of the duration

seconds

Returns full seconds of the duration

minutes

Returns full minutes of the duration

milliseconds

Returns milliseconds of the duration

year

Same as years. Returns full years of the duration

month

Returns trailing months of the duration

day

Returns trailing days of the duration

hour

Returns trailing hours of the duration

minute

Returns trailing minutes of the duration

second

Returns trailing seconds of the duration

millisecond

Returns trailing seconds of the duration

valueOf()

Same as milliseconds. Returns milliseconds of the duration

toString([mode[, threshold]])

Returns readable representation of the duration.
When invoked without arguments (defaults to mode=0), returns as:

10y 2m 6d 03:23:08.456

When invoked with mode 1, returns alternative representation:

10y 2m 6d 3h 23m 8s 456ms

Representation returned by default modes can be customized with threshold setting that trims lowest units:

duration.toString(); // 10y 2m 6d 03:23:08.456
duration.toString(0, 1); // 10y 2m 6d 03:23:08
duration.toString(0, 2); // 10y 2m 6d 03:23

duration.toString(1); // 10y 2m 6d 3h 23m 8s 456ms
duration.toString(1, 1); // 10y 2m 6d 3h 23m 8s
duration.toString(1, 2); // 10y 2m 6d 3h 23m

toString(format)

When invoked with string, formats the duration according to given pattern, where:

  • %y - duration.year
  • %m - duration.month
  • %d - duration.day
  • %H - duration.hour
  • %M - duration.minute
  • %S - duration.second
  • %L - duration.millisecond
  • %ms - duration.months
  • %ds - duration.days
  • %Hs - duration.hours
  • %Ms - duration.minutes
  • %Ss - duration.seconds
  • %Ls - duration.milliseconds
  • %sign - If duration is negative outputs - otherwise empty string

Tests

$ npm test