DEV Community

Cover image for Parse Duration Strings to Milliseconds with JavaScript
jQueryScript
jQueryScript

Posted on

Parse Duration Strings to Milliseconds with JavaScript

parse-duration-ms: a JavaScript library that converts human-readable duration strings into milliseconds.

Key features:

  • Parses combined units like "1h 30m" and "2 days 5 hours"
  • Supports decimal values, shortened forms, and case-insensitive input
  • Handles negative durations
  • Zero dependencies
  • Works in Node.js and browsers

Perfect for parsing timeout values, cache TTLs, rate limits, and configuration strings.

The library returns undefined for invalid input instead of throwing errors, which makes fallback handling cleaner.

πŸ‘‰ Blog Post

πŸ‘‰ GitHub Repo

πŸ‘‰ Live Demo

Top comments (0)