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 🙏

© 2025 – Pkg Stats / Ryan Hefner

comm-func

v1.0.3

Published

我的一些公共方法

Readme

时间处理工具库文档

目录

1. addZero

补零函数,用于将小于10的数字前面补零。

参数

|参数名|类型|说明| |--|--|--| |num|number|需要补零的数字|

返回值

string - 补零后的字符串

示例代码

addZero(5);  // 返回 "05"
addZero(12); // 返回 "12"

2. getNowTime

获取当前时间并格式化为指定格式。

参数

|参数名|类型|说明| |--|--|--| |format|string|时间格式,默认为 "YYYY-MM-DD HH:mm:ss"|

outType可选值

注意参数格式内容大小写可以随意,以作处理,除单独返回分钟类型改成ms

| 参数值 |类型| 说明 | |---------------------|--|------------------------------| | YYYY-MM-DD HH:mm:ss |string| 时间格式,默认值 2023-06-20 14:30:45 | | YYYY-MM-DD HH:mm |string| 2023-06-20 14:30 | | YYYY-MM-DD |string| 2023-06-20 | | YYYY-MM |string| 2023-06 | | hh:mm:ss |string| 14:30:45 | | hh:mm |string| 14:30 | | mm:ss |string| 30:45 | | YYYY |string| 2023 | | MM |string| 06(月份) | | DD |string| 20 | | hh |string| 14 | | mm |string| 30(分钟的参数值为mm) | | ss |string| 20 |

3.getTimeDifference

计算两个时间的差值。

参数

|参数名|类型|说明| |--|--|--| |startTime|Date | string|开始时间| |endTime|Date | string|结束时间| |format|string (可选)|指定返回格式,可选值见下表|

format可选值

|值|说明| |--|--| |days|天数差| |hours|小时差| |minutes|分钟差| |seconds|秒差| |milliseconds|毫秒差| |当不指定format时返回 object||

返回值对象结构

{
  milliseconds: number;  // 总毫秒数
  seconds: number;       // 剩余秒数(不足一分钟的部分)
  minutes: number;       // 剩余分钟数(不足一小时的部分)
  hours: number;         // 剩余小时数(不足一天的部分)
  days: number;          // 总天数
  totalSeconds: number;  // 总秒数
  totalMinutes: number;  // 总分钟数
  totalHours: number;    // 总小时数
  totalDays: number;     // 总天数
}

4.addTimeFormatted

在指定时间上添加时间单位并返回格式化字符串。

参数

参数说明

|参数名| 类型 |说明| |--|----------------------------|--| |date| Date \| string \| number |基础时间| |unit| string |要添加的时间单位,可选值见下表| |value| number |要添加的数值|

unit 可选值

|值|说明| |--|--| |millisecond|毫秒| |second|秒| |minute|分钟| |hour|小时| |day|天| |week|周| |month|月| |year|年|

返回值

  • string - 计算后的新时间,格式为 "yyyy-mm-dd hh:mm:ss"

示例代码

// 在当前时间上加5天
addTimeFormatted(new Date(), 'day', 5); 
// 返回示例: "2023-06-25 14:30:45"

// 在指定时间上加3个月
addTimeFormatted('2023-01-15', 'month', 3); 
// 返回示例: "2023-04-15 00:00:00"

使用示例

// 获取当前时间
const now = getNowTime('yyyy-mm-dd hh:mm:ss');

// 计算从现在到未来某天的时间差
const futureDate = addTimeFormatted(now, 'day', 7);
const diff = getTimeDifference(now, futureDate);

console.log(`现在时间: ${now}`);
console.log(`7天后时间: ${futureDate}`);
console.log(`相差天数: ${diff.days}`);