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

timelong

v1.5.6

Published

时间长度转换工具

Readme

简体中文 | English

badge badge badge badge

简介

1h2m3s4ms这种格式的字符串转为指定单位的时长

timelong.ms1h2m3s4ms => 3723004 (ms)
timelong.s1h2m3s4ms => 3723.004 (s)

使用

import timelong from "timelong";

console.log(timelong.ms1d);
const timelong = require('timelong').default;

console.log(timelong.ms1d)

属性名第一段是单位:

| 单位 | 含义 | | ---- | ---- | | w | 星期 | | d | 天 | | h | 小时 | | m | 分钟 | | s | 秒 | | ms | 毫秒 |

后面是具体的时间长度,数字后面跟的具体单位: 1d4m 表示1天4分钟。

timelong.m1d4m会换算成具体分钟数24*60+4=1444

也可以用中文,但不能中英文混用

中文单位有:

星期、周、天、日、小时、时、分钟、分、秒、毫秒

例如:

timelong.时1天

背景

代码中经常会有定义时长的常量,例如:

// axios默认超时时间
export const AXIOS_TIMEOUT_DEFAULT = 5000; // ms

简单的还好,还有稍微需要计算的需要在脑袋里过一遍,例如:

export const TASK_TIMEOUT_PERIOD = 20 * 60 * 1000; // ms

时间单位有可能是分钟、秒等非毫秒数,如果代码里不写注释和毫秒数的时间放在一起,可能会让人感到疑惑,例如:

// redis 过期时间
export const REDIS_EXPIRE_TIME = 7 * 24 * 60 * 60; // s

如果有一个工具能按照一定格式自动生成想要的时长,我们就能偷点懒了,可读性也强些。

上面的例子就会变成

export const AXIOS_TIMEOUT_DEFAULT = timelong.ms5s;
export const TASK_TIMEOUT_PERIOD = timelong.ms20m;
export const REDIS_EXPIRE_TIME = timelong.s7d;

示例

expect(timelong.ms1ms).toBe(1);
expect(timelong.ms1s).toBe(1 * 1000);
expect(timelong.ms1m).toBe(1 * 60 * 1000);
expect(timelong.ms1h).toBe(1 * 60 * 60 * 1000);
expect(timelong.ms1d).toBe(1 * 24 * 60 * 60 * 1000);
expect(timelong.ms1w).toBe(1 * 7 * 24 * 60 * 60 * 1000);
expect(timelong.ms12w34d56h78m9s10ms).toBe(
    12 * timelong.ms1w +
    34 * timelong.ms1d +
    56 * timelong.ms1h +
    78 * timelong.ms1m +
    9 * timelong.ms1s +
    10
);
expect(timelong.m1ms).toBeCloseTo(1 / 1000 / 60);
expect(timelong.m1s).toBeCloseTo(1 / 60);
expect(timelong.m1m).toBe(1);
expect(timelong.m1h).toBe(1 * 60);
expect(timelong.m1d).toBe(1 * 24 * 60);
expect(timelong.m1w).toBe(1 * 7 * 24 * 60);
expect(timelong.m12w34d56h78m9s10ms).toBeCloseTo(
    12 * timelong.m1w +
    34 * timelong.m1d +
    56 * timelong.m1h +
    78 * timelong.m1m +
    9 * timelong.m1s +
    10 * timelong.m1ms
);
expect(timelong.时1日).toBe(1 * 24);
expect(timelong.小时1日).toBe(1 * 24);
expect(timelong.时1天).toBe(1 * 24);
expect(timelong.小时1天).toBe(1 * 24);

以此类推

实现原理

Proxy解析属性名为具体的单位、星期数、天数、小时数、分数、秒数和毫秒数再累加相除