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 🙏

© 2025 – Pkg Stats / Ryan Hefner

js-time-conversion

v1.0.2

Published

A lightweight utility to convert time strings to milliseconds and vice versa.

Downloads

4

Readme

⏳ Time Converter (toMilliseconds & fromMilliseconds)

A simple and lightweight npm package to convert time strings to milliseconds and convert milliseconds back into readable formats.

npm

🚀 Features:

  • Convert time strings like "2h", "30m", "45s", "1.5d" into milliseconds.
  • Convert milliseconds into human-readable time formats.
  • Works with floating-point values ("1.5h"5400000 ms).
  • Lightweight with zero dependencies.

📦 Installation

You can install the package using npm:

npm install js-time-conversion

Or using Yarn:

yarn add js-time-conversion

🔧 Usage

Import the package in your JavaScript/Node.js project:

const { toMilliseconds, fromMilliseconds } = require("js-time-conversion");

Convert Time Strings to Milliseconds

console.log(toMilliseconds("2h"));   // 7200000
console.log(toMilliseconds("30m"));  // 1800000
console.log(toMilliseconds("45s"));  // 45000
console.log(toMilliseconds("1.5d")); // 129600000

Convert Milliseconds to Readable Time Format

console.log(fromMilliseconds(7200000));    // "2h"
console.log(fromMilliseconds(1800000));    // "30m"
console.log(fromMilliseconds(45000));      // "45s"
console.log(fromMilliseconds(129600000));  // "1.5d"

📜 API Reference

toMilliseconds(timeString)

Converts a time string to milliseconds.

| Parameter | Type | Description | Example | |------------|--------|--------------------------------------------------|---------| | timeString | string | Time string with unit (s, m, h, d) | "2h"7200000 |

  • Throws an error if the input format is invalid.

fromMilliseconds(ms)

Converts milliseconds to a human-readable time format.

| Parameter | Type | Description | Example | |-----------|---------|---------------------------------------------|---------| | ms | number | Milliseconds to be converted | 7200000"2h" |


🚨 Error Handling

If an invalid format is provided, the function throws an error:

console.log(toMilliseconds("xyz")); // ❌ Error: Invalid time format. Use '1s', '2m', '3h', '1.5d'.

🛠️ Running Tests

To test the package, create a file test.js and run:

const { toMilliseconds, fromMilliseconds } = require("./index");

// Example tests
console.log(toMilliseconds("2h") === 7200000); // true
console.log(fromMilliseconds(7200000) === "2h"); // true

Run the test:

node test.js

📌 Contributing

Want to improve this package? Feel free to fork and submit a pull request.

  1. Clone the repository:
    git clone https://github.com/Th85534/npm-packages.git
  2. Make changes and push to a new branch.
  3. Submit a Pull Request!

📜 License

This package is licensed under the MIT License.


🚀 Enjoy using Time Converter? Give it a ⭐ on GitHub!