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

yy-date-format

v1.0.0

Published

日期格式化工具,支持多种日期格式转换

Downloads

91

Readme

yy-date-format

日期格式化工具,支持多种日期格式转换。

安装

npm install yy-date-format

使用

基础用法

import { 
  formatTimestamp,
  formatDate,
  formatDateDash,
  formatDateTime,
  formatRelativeTime
} from 'yy-date-format';

// 格式化时间戳
const time = formatTimestamp(Date.now());
// "2024-01-01 12:00:00"

// 格式化日期(斜杠格式)
const date = formatDate(new Date());
// "2024/01/01"

// 格式化日期(横杠格式)
const dateDash = formatDateDash(new Date());
// "2024-01-01"

// 自定义格式
const datetime = formatDateTime(new Date(), 'YYYY-MM-DD HH:mm');
// "2024-01-01 12:00"

// 相对时间
const relative = formatRelativeTime(new Date(Date.now() - 3600000));
// "1小时前"

Excel日期转换

import { excelDateToJSDate } from 'yy-date-format';

// 将Excel日期序列号转换为Date对象
const date = excelDateToJSDate(44927); // Excel中的2023-01-01

获取今天的时间范围

import { getTodayTimeRange } from 'yy-date-format';

const [start, end] = getTodayTimeRange();
// ["2024-01-01 00:00:00", "2024-01-01 23:59:59"]

API

formatTimestamp(timestamp)

将毫秒时间戳转换为 YYYY-MM-DD HH:mm:ss 格式。

参数:

  • timestamp (number) - 毫秒时间戳

formatDate(value)

格式化日期为 YYYY/MM/DD 格式。

参数:

  • value (Date|string|number) - 日期值

formatDateDash(value)

格式化日期为 YYYY-MM-DD 格式。

参数:

  • value (Date|string|number) - 日期值

formatDateTime(value, format)

格式化日期时间。

参数:

  • value (Date|string|number) - 日期值
  • format (string) - 格式字符串,默认 'YYYY-MM-DD HH:mm:ss'

excelDateToJSDate(excelDate)

将 Excel 日期序列号转换为 Date 对象。

参数:

  • excelDate (number) - Excel日期序列号

getTodayTimeRange()

获取今天的开始和结束时间。

返回值:

  • string[] - [开始时间, 结束时间]

getDateFormat(days)

根据天数差获取日期格式。

参数:

  • days (number) - 天数差

返回值:

  • 'YY/MM' - 如果天数 > 90
  • 'YY/MM/DD' - 如果天数 <= 90

formatRelativeTime(date)

格式化相对时间(如:刚刚、1分钟前、1小时前等)。

参数:

  • date (Date|string|number) - 日期值

特性

  • ✅ 多种日期格式支持
  • ✅ Excel日期转换
  • ✅ 相对时间格式化
  • ✅ 零依赖
  • ✅ TypeScript 支持