Textual Time Period Converter
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.
Common errors
-
ERR_PACKAGE_PATH_NOT_EXPORTED
cause Node.js (or a bundler) failed to resolve the `to-time` package due to its `exports` map, often indicating an older Node.js version or incorrect module resolution configuration.fixUpdate Node.js to 20.19.0 or newer. If using TypeScript, set `moduleResolution` to `node16`, `nodenext`, or `bundler` in your `tsconfig.json`. -
TypeError: toTime is not a function
cause Attempting to use `toTime` after importing it incorrectly for the module context (e.g., `require` in an ESM file or `import` in a CJS file), or accessing the global `toTime` without the UMD build being loaded.fixIf 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.
Warnings
- breaking Version 3.0.0 introduced a dual CommonJS/ESM build strategy with an `exports` map in `package.json`. Projects on older Node.js versions (below 20.19.0) or with outdated bundler configurations may experience import resolution issues.
- gotcha This package explicitly requires Node.js version 20.19.0 or higher, as specified in its `engines` field. Running `to-time` on older Node.js versions may lead to runtime errors, incorrect module resolution, or unexpected behavior, especially due to the v3 `exports` map implementation.
- gotcha `to-time` leverages `bignumber.js` internally to guarantee high precision for all time calculations, mitigating floating-point inaccuracies. However, all getter methods (e.g., `ms()`, `seconds()`, `hours()`) return standard JavaScript `Number` types, not `BigInt` or `BigNumber` objects. Developers requiring `BigInt` precision for subsequent operations must manually convert the `Number` result.
Install
-
npm install to-time -
yarn add to-time -
pnpm add to-time
Imports
- toTime
const toTime = require('to-time');import toTime from 'to-time';
- toTime
import toTime from 'to-time';
const toTime = require('to-time'); - toTime
<script src="node_modules/to-time/lib/to-time.min.js"></script> <!-- exposes global: window.toTime -->
Quickstart
import toTime from 'to-time';
// Example 1: Parsing a textual time period and getting total days
// Converts "1 year and 6 months" to total days, demonstrating precision.
const daysInYearAndHalf = toTime('1 Year 6 Months').days();
console.log(`Days in 1 year and 6 months: ${daysInYearAndHalf}`);
// Expected output: Days in 1 year and 6 months: 547.5
// Example 2: Using factory methods for clearer time intervals in milliseconds
const twelveHoursInMs = toTime.fromHours(12).ms();
console.log(`12 hours in milliseconds: ${twelveHoursInMs}`);
// Expected output: 12 hours in milliseconds: 43200000
// Using it with setInterval/setTimeout for clear, human-readable delays.
// Simulate an action after 1.5 hours without blocking.
console.log('Simulating an action scheduled after 1.5 hours...');
setTimeout(() => {
console.log('Action performed after 1.5 hours.');
}, toTime.fromHours(1.5).ms());
// Example 3: Combining parsing, appending units, and humanizing the output
const combinedTime = toTime('2 days 3 hours').addMinutes(90).humanize();
console.log(`Combined time (2 days, 3 hours + 90 minutes) humanized: ${combinedTime}`);
// Expected output: Combined time (2 days, 3 hours + 90 minutes) humanized: 2 days, 4 hours, 30 minutes