npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

to-duration

v1.0.0

Published

A powerful collection of functions to convert a duration object into a number in any time unit

Downloads

72

Readme

to-duration

npm version install size npm downloads

A powerful collection of functions to convert a duration object into a number in any time unit.

Use toMilliseconds({ days: 3 }) instead of 3 * 24 * 60 * 60 * 1_000.

TypeScript-first, works with both ESM and CommonJS.

Installation

npm install to-duration

Examples

Recurring tasks

import { toMilliseconds } from 'to-duration';

setInterval(syncMetrics, toMilliseconds({ minutes: 5 }));

Long-running requests

import { toMilliseconds } from 'to-duration';

const response = await fetch('/api/heavy-report', {
	signal: AbortSignal.timeout(toMilliseconds({ seconds: 30 })),
});

S3 signed URLs

import { toSeconds } from 'to-duration';

import { GetObjectCommand } from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';

const command = new GetObjectCommand({
	Bucket: 'avatars',
	Key: 'users/42.webp',
});

const url = await getSignedUrl(s3, command, {
	expiresIn: toSeconds({ hours: 6 }),
});

API

Conversion functions:

toYears({ days: 365 }) // 1 year
toMonths({ days: 90 }) // 3 months
toWeeks({ days: 42 }) // 6 weeks
toDays({ weeks: 1 }) // 7 days
toHours({ days: 1 }) // 24 hours
toMinutes({ hours: 2 }) // 120 minutes
toSeconds({ minutes: 5 }) // 300 seconds
toMilliseconds({ seconds: 5 }) // 5 000 milliseconds
toMicroseconds({ milliseconds: 9 }) // 9 000 microseconds
toNanoseconds({ microseconds: 1 }) // 1 000 nanoseconds

Duration object

Functions accept an object with one or several units, at least one is required:

toSeconds({ days: 3, hours: 1, minutes: 1 }); // ✅ ok

toSeconds({}); // ❌ TypeScript error

Duration object type:

type Duration = {
	years?: number;
	months?: number;
	weeks?: number;
	days?: number;
	hours?: number;
	minutes?: number;
	seconds?: number;
	milliseconds?: number;
	microseconds?: number;
	nanoseconds?: number;
};

How conversion works

Functions use fixed, simplified units for year and month:

| Unit | Equals | |-------------|--------------------| | year | 365 days | | month | 30 days | | week | 7 days | | day | 24 hours | | hour | 60 minutes | | minute | 60 seconds | | second | 1 000 milliseconds | | millisecond | 1 000 microseconds | | microsecond | 1 000 nanoseconds |

It does not account for leap years, DST, or real calendar month lengths. For calendar-aware calculations, use a proper date library.

Tests

Tests cover every conversion direction. See the test suite.

Related

Has a similar feel to Temporal.PlainDate.prototype.add({ days: 5 }), which also accepts a duration-like object.

If you only need to convert to milliseconds, check out the excellent @sindresorhus/to-milliseconds with similar interface.

License

MIT