Logo

0x3d.Site

is designed for aggregating information.
Welcome
check repository here

ms

CI Edge Runtime Compatible

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

Examples

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

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

Time Format Written-Out

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

  • Works both in Node.js and in the browser
  • 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 support

As of v3.0, this package includes TypeScript definitions.

For added safety, we're using Template Literal Types (added in TypeScript 4.1). This ensures that you don't accidentally pass ms values that it can't process.

This won't require you to do anything special in most situations, but you can also import the StringValue type from ms if you need to use it.

import ms, { StringValue } from 'ms';

// Using the exported type.
function example(value: StringValue) {
  ms(value);
}

// This function will only accept a string compatible with `ms`.
example('1 h');

In this example, we use a Type Assertion to coerce a string.

import ms, { StringValue } from 'ms';

// Type assertion with the exported type.
function example(value: string) {
  try {
    // A string could be "wider" than the values accepted by `ms`, so we assert
    // that our `value` is a `StringValue`.
    //
    // It's important to note that this can be dangerous (see below).
    ms(value as StringValue);
  } catch (error: Error) {
    // Handle any errors from invalid values.
    console.error(error);
  }
}

// This function will accept any string, which may result in a bug.
example('any value');

You may also create a custom Template Literal Type.

import ms from 'ms';

type OnlyDaysAndWeeks = `${number} ${'days' | 'weeks'}`;

// Using a custom Template Literal Type.
function example(value: OnlyDaysAndWeeks) {
  // The type of `value` is narrower than the values `ms` accepts, which is
  // safe to use without coercion.
  ms(value);
}

// This function will accept "# days" or "# weeks" only.
example('5.2 days');

Advanced Usage

As of v3.0, you can import parse and format separately.

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
}

Edge Runtime Support

ms is compatible with the Edge Runtime. It can be used inside environments like Vercel Edge Functions as follows:

// Next.js (pages/api/edge.js) (npm i next@canary)
// Other frameworks (api/edge.js) (npm i -g vercel@canary)

import ms from 'ms';
const start = Date.now();

export default (req) => {
  return new Response(`Alive since ${ms(Date.now() - start)}`);
};

export const config = {
  runtime: 'experimental-edge',
};

Related Packages

  • ms.macro - Run ms as a macro at build-time.

Caught a Bug?

  1. Fork this repository to your own GitHub account and then clone it to your local device
  2. Link the package to the global module directory: npm link
  3. Within the module you want to test your local development instance of ms, just link it to the dependencies: npm link ms. Instead of the default one from npm, Node.js will now use your clone of ms!

As always, you can run the tests using: npm test

Node.js
Node.js
Node.js is a runtime environment for executing JavaScript on the server side. Known for its non-blocking architecture, it’s ideal for building fast and scalable network applications, including APIs and real-time services.
GitHub - davglass/license-checker: Check NPM package licenses
GitHub - davglass/license-checker: Check NPM package licenses
Build software better, together
Build software better, together
CodeSandbox
CodeSandbox
Node.js Design Patterns Third Edition by Mario Casciaro and Luciano Mammino
Node.js Design Patterns Third Edition by Mario Casciaro and Luciano Mammino
GitHub - almost/through2-concurrent: Simple Node.JS stream (streams2) Transform that runs the transform functions concurrently (with a set max concurrency)
GitHub - almost/through2-concurrent: Simple Node.JS stream (streams2) Transform that runs the transform functions concurrently (with a set max concurrency)
Google I/O 2013 - Accelerating Oz with V8: Follow the Yellow Brick Road to JavaScript Performance
Google I/O 2013 - Accelerating Oz with V8: Follow the Yellow Brick Road to JavaScript Performance
babel/packages/babel-parser at master · babel/babel
babel/packages/babel-parser at master · babel/babel
GitHub - SheetJS/sheetjs: 📗 SheetJS Spreadsheet Data Toolkit -- New home https://git.sheetjs.com/SheetJS/sheetjs
GitHub - SheetJS/sheetjs: 📗 SheetJS Spreadsheet Data Toolkit -- New home https://git.sheetjs.com/SheetJS/sheetjs
GitHub - sindresorhus/log-symbols: Colored symbols for various log levels
GitHub - sindresorhus/log-symbols: Colored symbols for various log levels
GitHub - Raathigesh/atmo: :heavy_check_mark: Mock data for your prototypes and demos. Remote deployments to Zeit now.
GitHub - Raathigesh/atmo: :heavy_check_mark: Mock data for your prototypes and demos. Remote deployments to Zeit now.
GitHub - tapio/live-server: A simple development http server with live reload capability.
GitHub - tapio/live-server: A simple development http server with live reload capability.
GitHub - pawurb/normit: Translations with speech synthesis in your terminal as a node package
GitHub - pawurb/normit: Translations with speech synthesis in your terminal as a node package
GitHub - lodash/lodash: A modern JavaScript utility library delivering modularity, performance, & extras.
GitHub - lodash/lodash: A modern JavaScript utility library delivering modularity, performance, & extras.
Node JS Internal Architecture | Ignition, Turbofan, Libuv
Node JS Internal Architecture | Ignition, Turbofan, Libuv
GitHub - chalk/chalk: 🖍 Terminal string styling done right
GitHub - chalk/chalk: 🖍 Terminal string styling done right
GitHub - sindresorhus/awesome-observables: Awesome Observable related stuff - An Observable is a collection that arrives over time.
GitHub - sindresorhus/awesome-observables: Awesome Observable related stuff - An Observable is a collection that arrives over time.
GitHub - patrickjuchli/basic-ftp: FTP client for Node.js, supports FTPS over TLS, passive mode over IPv6, async/await, and Typescript.
GitHub - patrickjuchli/basic-ftp: FTP client for Node.js, supports FTPS over TLS, passive mode over IPv6, async/await, and Typescript.
GitHub - codeceptjs/CodeceptJS: Supercharged End 2 End Testing Framework for NodeJS
GitHub - codeceptjs/CodeceptJS: Supercharged End 2 End Testing Framework for NodeJS
Newest 'node.js' Questions
Newest 'node.js' Questions
You Don't Know Node - ForwardJS San Francisco
You Don't Know Node - ForwardJS San Francisco
Node.js in Action, Second Edition
Node.js in Action, Second Edition
Google I/O 2009 - V8: ..High Performance JavaScript Engine
Google I/O 2009 - V8: ..High Performance JavaScript Engine
stackgl
stackgl
GitHub - MrRio/vtop: Wow such top. So stats. More better than regular top.
GitHub - MrRio/vtop: Wow such top. So stats. More better than regular top.
GitHub - nut-tree/nut.js: Native UI testing / controlling with node
GitHub - nut-tree/nut.js: Native UI testing / controlling with node
GitHub - mafintosh/pumpify: Combine an array of streams into a single duplex stream using pump and duplexify
GitHub - mafintosh/pumpify: Combine an array of streams into a single duplex stream using pump and duplexify
GitHub - sindresorhus/cli-cursor: Toggle the CLI cursor
GitHub - sindresorhus/cli-cursor: Toggle the CLI cursor
GitHub - sindresorhus/fkill-cli: Fabulously kill processes. Cross-platform.
GitHub - sindresorhus/fkill-cli: Fabulously kill processes. Cross-platform.
GitHub - retextjs/retext: natural language processor powered by plugins part of the @unifiedjs collective
GitHub - retextjs/retext: natural language processor powered by plugins part of the @unifiedjs collective
GitHub - sindresorhus/trash: Move files and directories to the trash
GitHub - sindresorhus/trash: Move files and directories to the trash
Node.js
More on Node.js

Programming Tips & Tricks

Code smarter, not harder—insider tips and tricks for developers.

Error Solutions

Turn frustration into progress—fix errors faster than ever.

Shortcuts

The art of speed—shortcuts to supercharge your workflow.
  1. Collections 😎
  2. Frequently Asked Question's 🤯

Tools

available to use.

Made with ❤️

to provide resources in various ares.