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

to-time

v1.0.2

Published

Utility for converting textual time periods to time units

Downloads

102,332

Readme

to-time

Build Status npm version

Utility for converting textual time periods to time units (milliseconds, seconds, minutes, hours, etc..).

######Due to the lack of precision in floating point numbers arithmetic and the need of keeping the results precise the utility is using the bignumber.js library for arithmetic operations on numbers.

Install

First install the package and save it to package.json using npm:

npm install --save to-time

To require in the browser:

Both files located in lib directory already include the bignumber.js dependency and there is no need to include this module in the browser.

<!-- access using window.toTime -->
<script src="node_modules/to-time/lib/to-time.min.js"></script>

To require when using NodeJS:

const toTime = require('to-time');

Usage

Converting from textual time period to time units

toTime('1 hour').seconds(); //3600
//same as:
toTime('1h').seconds(); //3600

toTime('1 Year 365 Days 4 Hours').hours(); //17524
//same as:
toTime('1y 365d 4h').hours(); //17524

Useful for usage in methods such as setInterval and setTimeout which consume the second argument in milliseconds, it is much clearer for someone who will read the code.

//Instead of using 43200000 milliseconds (equivalent to 12 hours) we can do the following
setInterval(() => {
  //Do something here
}, toTime('12h').ms());

//Instead of using 5400000 milliseconds (equivalent to 1.5 hour)
setTimeout(() => {
  //Do something here
}, toTime.fromHours(1.5).ms());
Allowed suffixes (all case-insensetive)
  • Year, Years, Y
  • Week, Weeks, W
  • Day, Days, D
  • Hour, Hours, H
  • Minute, Minutes, M
  • Second, Seconds, S
  • Millisecond, Milliseconds, MS

Initializing using factory methods

It is also possible to create a a TimeFrame instance by calling the static factory methods. The result will be a TimeFrame object similar to the one that is created by invoking the function with a textual time period.

toTime.fromHours(4).addMinutes(30).hours(); //4.5
toTime.fromYears(4).addWeeks(4).days(); //1488
Available factory methods
  • fromMilliseconds
  • fromSeconds
  • fromMinutes
  • fromHours
  • fromDays
  • fromWeeks
  • fromYears

Appenders methods

It is possible to add additional units to the TimeFrame object by invoking one of the appender methods on the returned instance:

toTime('0.5 hour').addMinutes(30).seconds(); //3600
toTime.fromHours(2).addMinutes(30).minutes(); //150
Available appenders:
  • addMilliseconds
  • addSeconds
  • addMinutes
  • addHours
  • addDays
  • addWeeks
  • addYears

Getters methods

The getter methods are used to get the value of the TimeFrame object in a specific time unit.

  • milliseconds : Number
  • ms (alias to milliseconds)
  • minutes : Number
  • hours : Number
  • days : Number
  • weeks : Number
  • years : Number
  • humanize : String

Converting the TimeFrame object to human readable format

It is also possible to use to-time in order to convert time units into human readable format. Example:

const frame = toTime.fromMilliseconds(500005050505005);
frame.humanize(); //15855 Years, 2 Weeks, 6 Days, 11 Hours, 48 Minutes, 25 Seconds, 5 Milliseconds

Contributing

Running tests
  • Make sure to write tests, run new & existing tests using:

    npm run test
  • Check for source code & tests code styling by running eslint:

    npm run lint
  • If tests are passing and eslint doesn't return any error -> Create pull request

To Do

  • [ ] Add Karma for testing in browser environment (Currently testing server side using Mocha)

License

MIT