
Security News
How Enterprise Security Is Adapting to AI-Accelerated Threats
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.
parse-duration
Advanced tools
convert a human readable duration to ms
With your favourite package manager:
packin add parse-durationcomponent install jkroso/parse-durationnpm install parse-durationthen in your app:
var parse = require('parse-duration')
convert str to ms
var ns = parse('1ns') // => 1 / 1e6
var μs = parse('1μs') // => 1 / 1000
var ms = parse('1ms') // => 1
var s = parse('1s') // => ms * 1000
var m = parse('1m') // => s * 60
var h = parse('1h') // => m * 60
var d = parse('1d') // => h * 24
var w = parse('1w') // => d * 7
var y = parse('1y') // => d * 365.25
It can also handle basic compound expressions
parse('1hr 20mins') // => 1 * h + 20 * m
whitespace
parse('1 hr 20 mins') // => 1 * h + 20 * m
And most other types of noise
parse('running length: 1hour:20mins') // => 1* h + 20 * m
You can even use negatives
parse('2hr -40mins') // => 1 * h + 20 * m
And exponents
parse('2e3s') // => 2000 * s
Available unit types are:
And its easy to add more
The 'ms' package is another utility for parsing and formatting milliseconds. It can convert human-readable strings into milliseconds and vice versa. Compared to parse-duration, 'ms' is more focused on simplicity and has a smaller feature set.
The 'moment-duration-format' package extends Moment.js to format durations. It is more feature-rich compared to parse-duration, offering advanced formatting options and integration with Moment.js for more complex date and time manipulations.
The 'humanize-duration' package is designed to convert milliseconds into human-readable strings. It is essentially the reverse of parse-duration, focusing on converting numerical durations into readable formats.
FAQs
Convert a human readable duration string to a duration format
The npm package parse-duration receives a total of 293,790 weekly downloads. As such, parse-duration popularity was classified as popular.
We found that parse-duration demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?

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.

Security News
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.