Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including readable-seconds with all npm packages installed. Try it out:

var readableSeconds = require("readable-seconds")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

readable-seconds v1.2.0

Convert seconds into human-readable time.

readable-seconds

Package to convert seconds into a readable format.

Install

$ npm install readable-seconds --save

Usage

Basic Usage

If a second argument is not provided then all units are returned.

const readableSeconds = require("readable-seconds");

console.log(readableSeconds(3690)); // 1 hour, 1 minute and 30 seconds

Limiting the number of units returned

An optional second argument can be used to limit the maximum number of units returned.

console.log(readableSeconds(3690, 2)); // 1 hour and 1 minute

Features

This module converts seconds into years, days, hours, minutes and seconds. A human-readable string is returned. Units with values of 0 are omitted and all the values returned are followed by the relevant unit type which will be appropriately singular or plural depending on the value. The units are separated by commas if there are more than 2 and the last value is preceded by the word "and".

An optional second argument can be provided to limit the number of units returned to the 2 largest units. This can be useful when dealing with large numbers of units where the smaller units are of less significance, as in the following example:

console.log(readableSeconds(31622490)); // 1 year, 1 day, 1 minute and 30 seconds
console.log(readableSeconds(31622490, 2)); // 1 year and 1 day

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free