Socket
Book a DemoInstallSign in
Socket

@alwatr/parse-duration

Package Overview
Dependencies
Maintainers
1
Versions
44
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@alwatr/parse-duration

A simple utility to parse a duration string into milliseconds number.

latest
Source
npmnpm
Version
5.5.19
Version published
Weekly downloads
1K
48.61%
Maintainers
1
Weekly downloads
 
Created
Source

Parse-duration

A simple, efficient utility to parse time duration strings into milliseconds or other time units.

Installation

npm install @alwatr/parse-duration
# or
yarn add @alwatr/parse-duration
# or
pnpm add @alwatr/parse-duration

Features

  • Parse duration strings (like '10s', '5m', '2h') into milliseconds
  • Convert between different time units
  • TypeScript support with full type safety
  • Zero dependencies (except for internal Alwatr utilities)
  • Optimized for performance
  • Comprehensive error handling

Usage

import {parseDuration} from '@alwatr/parse-duration';

// Basic usage - parse to milliseconds
parseDuration('10s');    // 10,000 (10 seconds in milliseconds)
parseDuration('5m');     // 300,000 (5 minutes in milliseconds)
parseDuration('2h');     // 7,200,000 (2 hours in milliseconds)
parseDuration('1.5d');   // 129,600,000 (1.5 days in milliseconds)

// Accept milliseconds as input
parseDuration(5000);     // 5,000 (pass through)

// Convert to different units
parseDuration('10d', 'h');  // 240 (10 days in hours)
parseDuration('1h', 'm');   // 60 (1 hour in minutes)
parseDuration('120s', 'm'); // 2 (120 seconds in minutes)
parseDuration(3600000, 'h'); // 1 (3600000 milliseconds in hours)

API

parseDuration(duration, toUnit?)

Parse a duration string or millisecond number into the specified unit (or milliseconds by default).

Parameters

  • duration: string | number - A duration string (like '10s', '5m') or a number (treated as milliseconds)
  • toUnit: DurationUnit (optional) - The unit to convert to. If omitted, returns milliseconds.

Returns

  • number: The duration in the specified unit (or milliseconds if no unit specified)

Throws

  • Error('not_a_number'): If the duration string doesn't contain a valid number
  • Error('invalid_unit'): If the unit is not recognized
  • Error('invalid_format'): If the duration format is invalid

Supported Units

UnitDescriptionMilliseconds Equivalent
sSecond1,000
mMinute60,000
hHour3,600,000
dDay86,400,000
wWeek604,800,000
MMonth (30d)2,592,000,000
yYear (365d)31,536,000,000

TypeScript Support

import {parseDuration, DurationUnit, Duration} from '@alwatr/parse-duration';

// Type-safe unit parameter
function waitFor(time: Duration): Promise<void> {
  const ms = parseDuration(time);
  return new Promise((resolve) => setTimeout(resolve, ms));
}

// Usage
await waitFor('5s');

Sponsors

The following companies, organizations, and individuals support Nanolib ongoing maintenance and development. Become a Sponsor to get your logo on our README and website.

Contributing

Contributions are welcome! Please read our contribution guidelines before submitting a pull request.

Keywords

alwatr

FAQs

Package last updated on 22 Sep 2025

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