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

time-to-seconds

v2.1.2

Published

Non-strict format, time to seconds converter

Downloads

398

Readme

time-to-seconds

Node.js CI codecov CodeFactor NPM Version npm install size npms.io (maintenance) npms.io (quality) npms.io (popularity) npms.io (final) semantic-release: angular NPM

Unopinionated time to seconds converter that lets you decide the format of your h:m:s; as long as you keep the number of colons between 0 and 2 it does not matter if you use "hh:mm:ss", "h:m:s" or "hhh:mmmm:ssss" format - you will always get the number of seconds back as an integer.

Documentation

Usage

Pass time as a string, e.g.:

  • "h:m:s"
  • "m:s"
  • ":s"
  • "s"
  • or any variation of "h:m:s", e.g. ":m:s"; as long as you keep the number of colons between 0 and 2, where h, m or s are any numbers, you will get seconds - as a number - in return.

Examples

var timeToSeconds = require("time-to-seconds");

timeToSeconds("2:2:2");
// => 7322

timeToSeconds("02:02:02");
// => 7322

timeToSeconds("2:02:02");
// => 7322

timeToSeconds("2:2:02");
// => 7322

timeToSeconds("2:");
// => 120

timeToSeconds("2:0");
// => 120

timeToSeconds("02:0");
// => 120

timeToSeconds("02:00");
// => 120

timeToSeconds(":2");
// => 2

timeToSeconds("2");
// => 2

timeToSeconds("0:2");
// => 2

timeToSeconds("0:02");
// => 2

timeToSeconds("00:02");
// => 2

timeToSeconds("");
// => 0

timeToSeconds("0");
// => 0

Empty Strings

The non-strict format will allow for the input of an empty string which will be treated the same as passing the number 0; the below inputs are equivalent and will return 0 seconds:

  • timeToSeconds("")
  • timeToSeconds("0")
  • timeToSeconds(":0")
  • timeToSeconds("::0")
  • timeToSeconds(":")
  • timeToSeconds("::")

Error Handling

timeToSeconds("Anything else than time string");
// TypeError: 'time-to-seconds: wrong argument type - something else than a number string in format "number", "number:number" or "number:number:number" was passed. See documentation for more information on argument formatting: https://www.npmjs.com/package/time-to-seconds.'

timeToSeconds("2:2:2:2");
// TypeError: 'time-to-seconds: too many colons - make sure the function argument is a number string in format "number", "number:number" or "number:number:number". See documentation for more information on argument formatting: https://www.npmjs.com/package/time-to-seconds.'

Decimal Numbers

If you pass decimals to the function, e.g.:

  • timeToSeconds("0.2:00") or
  • timeToSeconds("0.2:")

(in the above examples the inputs are the same) it will be interpreted as 0.2 of one minute, so 12 seconds (60 * 0.2 = 12 seconds).

Similarly, if you pass, e.g.:

  • timeToSeconds("0.2:00:00") or
  • timeToSeconds("0.2::")

(again, the inputs are the same) it will be interpreted as 0.2 of one hour, so 720 seconds (3600 * 0.2 = 720 seconds).

Together:

  • timeToSeconds("0.2:0.2:00") or
  • timeToSeconds("0.2:0.2:")

will return 732 seconds (0.2 of one hour is 720 seconds, 0.2 of one minute is 12 seconds; 720 + 12 = 732 seconds)

Note: passing decimals as seconds, e.g.: timeToSeconds("0:0:0.2"), will simply return 0.2 seconds.

Math

You could calculate the value to convert and because of the non-strict format, pass the calcualted value to the function, e.g.:

var num = Math.log10(100);
timeToSeconds(`${num.toString()}:`);
// => 120

timeToSeconds(`${Math.log10(100).toString()}:`);
// => 120

License

MIT