New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

milli

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

milli

Tiny millisecond conversion utility

  • 4.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

milli, fork of ms

Use this package to easily convert various time formats to milliseconds.

It should be compatible with all modern JS runtimes.

It is a fork of the original ms package, which is abandoned with the latest (canary) version release being almost 3 years old.

Examples

import { ms } from "milli"

ms("2 days")  // 172800000
ms("1d")      // 86400000
ms("10h")     // 36000000
ms("2.5 hrs") // 9000000
ms("2h")      // 7200000
ms("1m")      // 60000
ms("5s")      // 5000
ms("1y")      // 31557600000
ms("100")     // 100
ms("-3 days") // -259200000
ms("-1h")     // -3600000
ms("-200")    // -200

Convert from Milliseconds

import { ms } from "milli"

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

Time Format Written-Out

import { ms } from "milli"

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"

Features

  • If a number is supplied to ms, a string with a unit is returned
  • If a string that contains the number is supplied, it returns it as a number (e.g.: it returns 100 for "100")
  • If you pass a string with a number and a valid unit, the number of equivalent milliseconds is returned

TypeScript

If you want to pass a string variable into ms, you will need a type coercion to string:

import { ms } from "milli"

ms("1h" as never)

This is because of the literal types used for DevX, so you get proper completion in your IDE.

You can also use parse instead, which is a less strict version of passing a string into ms.

Advanced Usage

import { parse, format } from "ms"

parse("1h") // 3600000

format(2000) // "2s"

If you want strict type checking for the input value, you can use parseStrict.

import { parseStrict } from "ms"

parseStrict("1h") // 3600000

function example(s: string) {
  return parseStrict(str) // tsc error
}

FAQs

Package last updated on 18 May 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc