首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >nutpi-calendar 中国传统日历相关功能插件(适配鸿蒙)

nutpi-calendar 中国传统日历相关功能插件(适配鸿蒙)

作者头像
徐建国
发布2025-06-11 13:22:39
发布2025-06-11 13:22:39
24200
代码可运行
举报
文章被收录于专栏:个人路线个人路线
运行总次数:0
代码可运行

nutpi-calendar 是一个功能强大的 UTS 插件,用于处理公历和农历之间的转换,获取节日信息、节气、生肖、干支纪年等中国传统日历相关功能。旨在为 uni-app 开发者提供一个简单易用且高效的日历解决方案,尤其适用于需要深度集成中国农历功能的应用。

特性

  • 公历转农历:轻松将公历日期转换为详细的农历信息,包括农历年份、月份、日期、是否闰月、生肖、天干地支等。
  • 农历转公历:支持将农历日期反向转换为公历日期。
  • 节日信息:内置公历节日和农历传统节日数据,方便获取指定日期的节日名称。
  • 节气计算:准确计算二十四节气。
  • 自定义节日:支持添加或修改节日信息。
  • 高性能:基于 UTS 实现,保证在 App 端的原生性能。

API 文档

插件导出一个名为 calendar 的对象,包含以下主要方法:

  • solar2lunar(year: number, month: number, day: number): LunarDateInfo
    • lunarYear: number - 农历年份
    • lunarMonth: number - 农历月份
    • lunarDay: number - 农历日期
    • isLeap: boolean - 是否闰月
    • lunarYearChn: string - 农历年份中文 (例如: "甲辰")
    • lunarMonthChn: string - 农历月份中文 (例如: "正月")
    • lunarDayChn: string - 农历日期中文 (例如: "初一")
    • animal: string - 生肖 (例如: "龙")
    • astro: string - 星座
    • gzYear: string - 干支年 (例如: "甲辰")
    • gzMonth: string - 干支月
    • gzDay: string - 干支日
    • solarTerm: string - 当日节气,若无则为空字符串
    • festival: string - 公历节日,若无则为空字符串
    • lunarFestival: string - 农历节日,若无则为空字符串
    • year: 公历年份 (例如: 2024)
    • month: 公历月份 (1-12)
    • day: 公历日期 (1-31)
    • 描述:将公历日期转换为农历日期信息。
    • 参数:
    • 返回值 LunarDateInfo 对象,包含:
  • lunar2solar(lunarYear: number, lunarMonth: number, lunarDay: number, isLeapMonth: boolean = false): SolarDateInfo
    • year: number - 公历年份
    • month: number - 公历月份
    • day: number - 公历日期
    • weekday: number - 星期几 (0 为周日,1 为周一,以此类推)
    • weekdayChn: string - 星期几中文
    • lunarYear: 农历年份
    • lunarMonth: 农历月份
    • lunarDay: 农历日期
    • isLeapMonth: boolean (可选, 默认为 false) - 是否为闰月
    • 描述:将农历日期转换为公历日期信息。
    • 参数:
    • 返回值 SolarDateInfo 对象,包含:
  • getFestival(year: number, month: number, day: number): string
    • 描述:获取指定公历日期的公历节日名称。
    • 参数:同 solar2lunar
    • 返回值:节日名称字符串,若无则为空字符串。
  • getLunarFestival(lunarYear: number, lunarMonth: number, lunarDay: number): string
    • 描述:获取指定农历日期的农历节日名称。
    • 参数:同 lunar2solar (无需 isLeapMonth)
    • 返回值:节日名称字符串,若无则为空字符串。
  • setFestival(month: number, day: number, festivalName: string): void
    • month: 公历月份
    • day: 公历日期
    • festivalName: 节日名称。如果传入空字符串,则表示删除该节日。
    • 描述:设置或覆盖公历节日。
    • 参数:
  • setLunarFestival(lunarMonth: number, lunarDay: number, festivalName: string): void
    • lunarMonth: 农历月份
    • lunarDay: 农历日期
    • festivalName: 节日名称。如果传入空字符串,则表示删除该节日。
    • 描述:设置或覆盖农历节日。
    • 参数:
  • getTerm(year: number, month: number, day: number): string
    • 描述:获取指定公历日期的节气名称 (此方法实际通过 solar2lunar 的返回值的 solarTerm 属性获取)。
  • toGanZhiYear(year: number): string
    • 描述:获取指定公历年份的干支纪年。
  • getAnimal(year: number): string
    • 描述:获取指定公历年份的生肖。

使用案例

以下是如何在您的 uni-app x 项目中使用 nutpi-calendar 插件:

引入插件

在您的 .uvue.uts 文件中引入插件:

代码语言:javascript
代码运行次数:0
运行
复制
import calendar from "@/uni_modules/nutpi-calendar";

调用 API

代码语言:javascript
代码运行次数:0
运行
复制
// 在页面的 script 部分
exportdefault {
  onLoad() {
    // 公历转农历
    const today = newDate();
    const lunarDate = calendar.solar2lunar(
      today.getFullYear(),
      today.getMonth() + 1,
      today.getDate()
    );
    console.log(
      `今天是农历:${lunarDate.lunarYearChn}年 ${lunarDate.lunarMonthChn}${lunarDate.lunarDayChn}`
    );
    console.log(`生肖:${lunarDate.animal}`);
    if (lunarDate.solarTerm) {
      console.log(`节气:${lunarDate.solarTerm}`);
    }
    if (lunarDate.festival) {
      console.log(`公历节日:${lunarDate.festival}`);
    }
    if (lunarDate.lunarFestival) {
      console.log(`农历节日:${lunarDate.lunarFestival}`);
    }

    // 农历转公历 (例如:查询2024年正月初一的公历日期)
    const solarDate = calendar.lunar2solar(2024, 1, 1);
    console.log(
      `2024年正月初一的公历日期是:${solarDate.year}-${solarDate.month}-${solarDate.day}`
    );

    // 获取指定日期的节日
    const christmas = calendar.getFestival(today.getFullYear(), 12, 25);
    console.log(`12月25日是:${christmas}`);

    const springFestival = calendar.getLunarFestival(2025, 1, 1); // 查询2025年春节
    console.log(`2025年农历正月初一是:${springFestival}`);

    // 设置自定义节日
    calendar.setFestival(10, 24, "程序员节");
    const programmersDay = calendar.getFestival(
      today.getFullYear(),
      10,
      24
    );
    console.log(`10月24日是:${programmersDay}`);
  },
};

版权信息

  • 作者:坚果派
  • 公众号:nutpi
  • 电话:17752170152
  • 官网:https://www.nutpi.net/[1]

相关链接

  • UTS 语法[2]
  • UTS API 插件[3]
  • Hello UTS[4]
  • 插件地址:https://ext.dcloud.net.cn/plugin?id=23786
  • 仓库地址:https://gitcode.com/nutpi/uni-calendar

坚果派 uni-app X 插件

  1. nutpi-idcard:用于解析身份证号码,提取其中的关键信息,如地区、出生日期、性别等。支持中国居民身份证、港澳台居民居住证以及外国人永久居留身份证的插件。插件地址:https://ext.dcloud.net.cn/plugin?id=23728,仓库地址:https://gitcode.com/nutpi/uni-idcard。
  2. nutpi-chinese-number-format:一个功能强大的中文数字格式化 UTS 插件,支持数字与中文之间的双向转换,适用于 uni-app 和 uni-app x 项目。插件地址:https://ext.dcloud.net.cn/plugin?id=23776,仓库地址:https://gitcode.com/nutpi/uni-chinese-number-format/。
  3. nutpi-utils:支持鸿蒙的拨打电话,发短信、退出应用、电量获取等功能。插件地址:https://ext.dcloud.net.cn/plugin?id=22525,仓库地址:https://gitcode.com/nutpi/uni-utils。
  4. calendar 中国传统日历nutpi-calendar 是一个功能强大的 UTS 插件,用于处理公历和农历之间的转换,获取节日信息、节气、生肖、干支纪年等中国传统日历相关功能。插件地址:https://ext.dcloud.net.cn/plugin?id=23786,仓库地址:https://gitcode.com/nutpi/uni-calendar。

参考资料

[1]

https://www.nutpi.net/: https://www.nutpi.net/

[2]

UTS 语法: https://uniapp.dcloud.net.cn/tutorial/syntax-uts.html

[3]

UTS API 插件: https://uniapp.dcloud.net.cn/plugin/uts-plugin.html

[4]

Hello UTS: https://gitcode.net/dcloud/hello-uts

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2025-06-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 大前端之旅 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 特性
  • API 文档
  • 使用案例
  • 版权信息
  • 相关链接
  • 坚果派 uni-app X 插件
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档