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

itcast-relative-time

v1.1.0

Published

轻量级相对时间格式化工具,将时间戳/Date对象转为人性化文本

Readme

itcast-relative-time

轻量级相对时间格式化工具,集成 dayjs 日期库,将时间戳/Date 对象转换为“刚刚、X分钟前”等人性化文本,适用于评论、订单、消息等时间展示场景。

安装

npm install itcast-relative-time

使用

// ES Module 导入
import formatRelativeTime from 'yourname-relative-time'

// 示例 1:传入 Date 对象
console.log(formatRelativeTime(new Date()))
// 输出:刚刚

// 示例 2:传入毫秒级时间戳
console.log(formatRelativeTime(Date.now() - 180 * 1000))
// 输出:3 分钟前

// 示例 3:传入秒级时间戳
console.log(formatRelativeTime(1735324800))
// 输出:对应的相对时间

时间粒度规则

| 时间范围 | 显示文本 | | ----------- | ----- | | ≤ 10 秒 | 刚刚 | | 10 秒 ~ 1 分钟 | X 秒前 | | 1 分钟 ~ 1 小时 | X 分钟前 | | 1 小时 ~ 1 天 | X 小时前 | | 1 天 ~ 30 天 | X 天前 | | 30 天 ~ 1 年 | X 个月前 | | > 1 年 | X 年前 |

参数说明

  • time(必填):待格式化时间,支持 Date 对象、毫秒级时间戳、秒级时间戳。
  • 返回值:格式化后的相对时间文本字符串。