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-formatter

v1.0.7

Published

Simple duration formatting to turn seconds into strings representing lengths of time.

Downloads

280

Readme

Duration Formatter and Parser

Simple duration formatting to turn seconds into strings representing lengths of time.

i.e.

timeFromSeconds(5053) returns "01:24:13"

and vice versa:

secondsFromTime("01:24:13") returns 5053

Basic Usage

Basic usage is illustrated with just two functions, timeFromSeconds and secondsFromTime:

import {timeFromSeconds, secondsFromTime} from 'duration-formatter';
 
var time_string = timeFromSeconds(5053);
 
console.log( time_string );

var parsed_seconds = secondsFromTime('01:24:13');

console.log( parsed_seconds );

Which outputs:

'01:24:13' 
5053

Using Custom Templates

By default, timeFromSeconds returns a string in the format: [Hours]:[Minutes]:[Seconds]

This can be changed by passing a function to the output_template option like so:

var input_time = 5053;

var output_template = (hours,minutes,seconds)=>  hours+'H '+minutes+'M '+seconds+'S';
var time_str = timeFromSeconds(input_time, { output_template });

console.log(time_str);

This outputs the a string in the new output format: 01H 24M 13S

To convert strings in your specified format back to a number you can specify the template_string option to the secondsFromTime function like so:

var template_string = "{H}H {M}M {S}S";
var seconds = secondsFromTime(time_str, {template_string});

console.log(seconds);

This outputs the original input time. In this case 5053.

Functions

timeFromSeconds(seconds, options = {})

seconds: Number

options: Object containing any combination of the following options:

  • hours_padding: Integer (defaults to 2)

  • minutes_padding: Integer (defaults to 2)

  • seconds_padding: Integer (defaults to 2)

  • seconds_decimal_places: Integer (defaults to 0)

  • decimal_symbol: String (defaults to ",")

  • output_template: Function that takes parameters determines the formatting of the parsed seconds. Defaults to (hours, minutes, seconds)=> `${ hours }:${ minutes }:${ seconds }` Note that each parameter is a string, so you will have to parse them to numbers if you wish to combine them.


secondsFromTime(time_string, options = {})

time_string: String in the format returned by the default output template of timeFromSeconds

options: Object containing any combination of the following options:

  • decimal_symbol: String (defaults to ",")
  • template_string String (defaults to "{H}:{M}:{S}")