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 🙏

© 2024 – Pkg Stats / Ryan Hefner

dayjs-plugin-lunar

v1.3.0

Published

[![NPM Version](https://img.shields.io/npm/v/dayjs-plugin-lunar)](https://npmjs.com/package/dayjs-plugin-lunar) [![NPM Downloads](https://img.shields.io/npm/dm/dayjs-plugin-lunar)](https://npmcharts.com/compare/dayjs-plugin-lunar?minimal=true) ![NPM Licen

Downloads

205

Readme

Dayjs Plugin Lunar

NPM Version NPM Downloads NPM License

dayjs-plugin-lunar 是一个基于 Day.js 的扩展插件,用于方便地处理和转换农历时间。它依赖 tyme4ts 作为底层时间计算库。

安装

npm install dayjs dayjs-plugin-lunar

如果你的 npm 版本低于 7,则需要额外安装 tyme4ts:

npm install tyme4ts

快速开始

基础使用

import dayjs from 'dayjs';
import { PluginLunar } from 'dayjs-plugin-lunar';

// 注册插件
dayjs.extend(PluginLunar);

dayjs('2024-12-19 12:00:00').format('LMLDLhLK') // 冬月十九午正初刻

// 转换为农历时辰
dayjs('2024-12-19 12:00:00').toLunarHour().getName(); // 午时

// 转换为农历日期
dayjs('2024-12-19 12:00:00').toLunarDay().getName(); // 十九

// 转换为农历月份
dayjs('2024-12-19 12:00:00').toLunarMonth().getName(); // 冬月

// 通过农历构造日期
dayjs.lunar(2025, 1, 1).format('YYYY-MM-DD') // 2025-01-29

自定义配置

tyme4ts 将农历的 冬月腊月 表述为 十一月十二月dayjs-plugin-lunar 希望更遵循传统命名,对此进行了修改。若需显示 tyme4ts 的原始表述,可将 traditional 设置为 false

dayjs.extend(PluginLunar, { traditional: false });

// 转换为农历月份(使用 `tyme4ts` 原始表述)
dayjs.lunar(2024, 11, 1).toLunarMonth().getName(); // 十一月

更多用法参看测试用例

API 参考

format

基于 dayjs 原始 format 方法拓展,新增农历格式化模板

扩展的 format 模版

| 模板 | 输出 | 详情 | |----|-----------------|----------| | LY | 甲子 乙丑 ... 癸亥 | 干支纪年 | | LM | 正月 二月 ... 腊月 | 农历月份 | | LD | 初一 初二 ... 三十 | 农历日期 | | LH | 子时 丑时 ... 亥时 | 农历时辰 | | Lh | 子 丑 ... 亥 | 农历时辰(简写) | | LK | 初初刻 初一刻 ... 正三刻 | 农历刻数(采用九十六刻制) |

add / subtract

基于 dayjs 原始 add / subtract 方法拓展,新增农历单位加减

| 单位 | 详情 | |-----------------|------| | lunar-year | 农历年 | | lunar-month | 农历月 | | lunar-day | 农历日 | | lunar-dual-hour | 农历时辰 |

lunar

使用农历时间创建 Dayjs 对象

Parameters

  • lunarYear number 农历年
  • lunarMonth number? 农历月,1 到 12,闰月为负数
  • lunarDay number? 农历日,1 到 30
  • lunarHour number? 时,0 到 23
  • lunarMinute number? 分,0 到 59
  • lunarSecond number? 秒,0 到 59

Examples

dayjs.lunar(2025, 1, 1).format('YYYY-MM-DD') // 2025-01-29

Returns Dayjs

toLunarHour

转换为农历的时辰

Examples

dayjs('2024-12-19 12:00:00').toLunarHour().getName(); // 午时

Returns LunarHour tyme4ts 中的 LunarHour

toLunarDay

转换为农历的日期

Examples

dayjs('2025-01-29').toLunarDay().getName(); // 初一

Returns LunarDay tyme4ts 中的 LunarDay

toLunarMonth

转换为农历的月份

Examples

dayjs('2025-01-29').toLunarMonth().getName(); // 正月

Returns LunarMonth tyme4ts 中的 LunarMonth

toLunarSeason

转换为农历的季节

Examples

dayjs('2025-01-29').toLunarSeason().getName(); // 孟春

Returns LunarSeason tyme4ts 中的 LunarSeason

toLunarYear

转换为农历的年份

Examples

dayjs('2025-01-01').toLunarYear().getYear(); // 2024

Returns LunarYear tyme4ts 中的 LunarYear

addLunar

添加农历时间

Parameters

  • value number 数量
  • unit LunarUnit 单位

Examples

dayjs('2024-12-20 12:00:00').addLunar(1, 'dual-hour').toLunarHour().getName(); // 未时

Returns Dayjs

subtractLunar

减去农历时间,与 addLunar 相反

Parameters

  • value number 数量
  • unit LunarUnit 单位

Examples

dayjs('2024-12-20 12:00:00').subtractLunar(1, 'dual-hour').toLunarHour().getName(); // 巳时

Returns Dayjs

License

MIT