Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
ms | 2.0.0 | 2.81 kB | MIT | prod |
The 'ms' npm package is a diminutive utility designed for the express purpose of converting various time formats into milliseconds. This tool is handy in situations where calculations or operations integral to the function of your program or application require input in the form of milliseconds. The 'ms' package works on both Node.js and in-browser environments, robustly interpreting a variety of time expressions. For example, it can convert full written-out time durations - '2 days', '1 hour', etc - and abbreviated inputs - '1d', '10h', etc.
Usage of the 'ms' library is refreshingly straightforward. It can be invoked within your JavaScript code by simply calling the ms()
function. This function takes one mandatory argument, which is the time duration (in any format) that you want to convert to milliseconds. Additionally, there is an optional argument that configures whether the output should be in a long-fully-written-out format.
Here are examples to demonstrate how to use 'ms':
To get the equivalent milliseconds for a time duration:
const ms = require('ms');
console.log(ms('2 days')); // Outputs: 172800000
console.log(ms('1d')); // Outputs: 86400000
console.log(ms('10h')); // Outputs: 36000000
To convert from milliseconds to time duration:
console.log(ms(60000)); // Outputs: "1m"
console.log(ms(2 * 60000)); // Outputs: "2m"
console.log(ms(-3 * 60000)); // Outputs: "-3m"
console.log(ms(ms('10 hours'))); // Outputs: "10h"
To get the converted duration in a long format:
console.log(ms(60000, { long: true })); // Outputs: "1 minute"
console.log(ms(2 * 60000, { long: true })); // Outputs: "2 minutes"
console.log(ms(-3 * 60000, { long: true })); // Outputs: "-3 minutes"
console.log(ms(ms('10 hours'), { long: true })); // Outputs: "10 hours"
The 'ms' package's documentation can be found within its readme on the official GitHub page of the package. The link to the page is https://github.com/vercel/ms. The readme contains comprehensive information on the usage of the 'ms' package in various cases, as well as details on features and examples of code snippets.