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 🙏

© 2026 – Pkg Stats / Ryan Hefner

true-ms

v1.1.0

Published

A Millisecond conversion utility with true precision

Readme

true-ms

CI NPM Version NPM Bundle Size Edge Runtime Compatible

true-ms is a robust JavaScript utility package designed to accurately convert various time formats into a precise number of milliseconds. It handles durations, dates, and even leap years, making it a reliable choice for time-sensitive applications.


👀 Features

  • Leap Year Awareness: Accurately calculates durations, considering leap years for precise results. (Read: Gregorian calendar and Common year)
  • Universal Compatibility: Works seamlessly across Node.js, Edge Runtime, and modern browsers.
  • Flexible Input/Output:
    • When a number (milliseconds) is supplied, a human-readable string with a unit is returned (e.g., ms(60000) returns "1m").
    • When a string containing only a number is provided, it's converted and returned as a number (e.g., ms('100') returns 100).
    • When a string with a number and a valid unit is passed, the equivalent number of milliseconds is returned (e.g., ms('2 days') returns 172800000).

📦 Installation

npm install true-ms

pnpm

pnpm install true-ms

yarn

yarn add true-ms

🚀 Usage

The ms function is the primary export. It can take a string, a number, or an object as input, along with an optional options object.

import ms from 'true-ms';

// Convert a duration string to milliseconds
const milliseconds = ms('1 day'); // 86400000

// Convert milliseconds to a duration string
const durationString = ms(3600000); // "1h"

// Convert a duration object to milliseconds
const objectMilliseconds = ms({ hours: 2, minutes: 30 }); // 9000000

// Convert with a starting date for accurate month/year calculations
const dateMilliseconds = ms('1 month', { from: new Date('2024-02-01') }); // Handles leap year for Feb 2024

📝 Examples

import ms from 'true-ms';

// Basic conversions
ms('2 days')  // 172800000
ms('1d')      // 86400000
ms('10h')     // 36000000
ms('2.5 hrs') // 9000000
ms('2h')      // 7200000
ms('1m')      // 60000
ms('1mon')    // 2678400000 (approx. 31 days)
ms('5s')      // 5000
ms('1y')      // 31557600000 (approx. 365.25 days)
ms('100')     // 100

// Negative durations
ms('-3 days') // -259200000
ms('-1h')     // -3600000
ms('-200')    // -200

Convert from Duration Object

import ms from 'true-ms';

ms({ hours: 10 })          // 36000000
ms({ days: 2 })            // 172800000
ms({ minutes: 1 })         // 60000
ms({ seconds: 5 })         // 5000
ms({ years: 1 })           // 31536000000
ms({ months: 1, days: 1 }) // 2764800000 (e.g., 31 days for month + 1 day)
ms({ years: 1, months: 2, days: 3, hours: 4, minutes: 5, seconds: 6 }) // Combined duration

Convert from a Date with Context

The from option allows for precise calculations based on a specific starting date, crucial for accurate month and year durations, especially around leap years.

import ms from 'true-ms';

ms('1mon', { from: new Date('2016-01-01') }) // 2678400000 (31 days in Jan 2016)
ms('1mon', { from: new Date('2017-02-01') }) // 2419200000 (28 days in Feb 2017)
ms('1y', { from: new Date('2016-01-01') })   // 31622400000 (Leap year; 366 days)
ms('1y', { from: new Date('2015-01-01') })   // 31536000000 (Non-leap year; 365 days)
ms('1mon', { from: new Date('2024-02-01') }) // 2505600000 (29 days in Feb 2024 - a leap year)

Convert from Milliseconds to String

import ms from 'true-ms';

ms(60000)          // "1m"
ms(2 * 60000)      // "2m"
ms(-3 * 60000)     // "-3m"
ms(ms('10 hours')) // "10h"

Time Format Written-Out (Long Format)

Use the { long: true } option to get a more descriptive string.

import ms from 'true-ms';

ms(60000, { long: true })          // "1 minute"
ms(2 * 60000, { long: true })      // "2 minutes"
ms(-3 * 60000, { long: true })     // "-3 minutes"
ms(ms('10 hours'), { long: true }) // "10 hours"

📚 Documentation

For all configuration options and detailed API reference, please see the API docs.

🚀 Migrations

Migration from vercel/ms

The true-ms package is designed to be fully compatible with vercel/ms. To migrate, simply remove ms from your dependencies and replace it with true-ms.

- import ms from 'ms';
+ import ms from 'true-ms';

🤝 Contributing

Want to contribute? Awesome! To show your support is to star the project, or to raise issues on GitHub.

Thanks again for your support, it is much appreciated! 🙏

License

MIT © Shahrad Elahi