to-time is a JavaScript/TypeScript utility for parsing and converting textual time periods (e.g., "1 hour", "1y 365d 4h") into various standard time units such as milliseconds, seconds, minutes, hours, days, weeks, and years. The current stable version is 3.0.2, with recent minor and patch releases indicating active development. A key differentiator of to-time is its internal reliance on the `bignumber.js` library to perform arithmetic operations, ensuring high precision and mitigating common floating-point inaccuracies often encountered in time calculations. The package supports dual CommonJS and ESM builds through an `exports` map, providing native compatibility for modern Node.js environments (requiring Node.js 20.19+). It also ships with comprehensive TypeScript types, facilitating robust development. The library offers both a primary parsing function and a suite of static factory methods (`fromMilliseconds`, `fromHours`) for initialization, alongside fluent appender methods (`addMinutes`, `addYears`) for manipulating time frames.
npm install to-timeVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates parsing time strings, using factory methods to create `TimeFrame` instances, chaining appender methods, and retrieving values in various units, including human-readable formats. It highlights practical usage for setting time-based operations like `setTimeout`.
Upgrade Node.js to 20.19.0 or higher. For TypeScript, ensure `moduleResolution` is set to `node16`, `nodenext`, or `bundler` in `tsconfig.json`. Verify your bundler (e.g., Webpack, Rollup) is configured to correctly resolve `package.json` `exports` fields.
Ensure your Node.js development and deployment environments meet the minimum requirement of 20.19.0. Tools like `nvm` (Node Version Manager) can help manage multiple Node.js versions efficiently.
Be aware that the output of getter methods is always a `Number`. If `BigInt` precision is required after `to-time` calculations, explicitly convert the `Number` result to `BigInt` using `BigInt(value)`.
Update Node.js to 20.19.0 or newer. If using TypeScript, set `moduleResolution` to `node16`, `nodenext`, or `bundler` in your `tsconfig.json`.
If your `package.json` has `"type": "module"` or it's an `.mjs` file, use `import toTime from 'to-time';`. If it's a CommonJS module (no `"type": "module"` and a `.js` file), use `const toTime = require('to-time');`. For browsers without a bundler, ensure `to-time.min.js` is loaded via a script tag.