更新记录

2.2.0(2025-10-22) 下载此版本

v2.2.0 更新内容:

 ✨ 新功能
 - 组件自动记忆功能(LocalStorage)
 - 类型切换显示当前时间
 - 简化日志输出(中文)

2.1.0(2025-10-22) 下载此版本

yi-datetime-picker 更新日志

v2.1.0 (2025-10-22)

✨ 新功能

  1. 实现 minDate/maxDate 日期范围限制

平台兼容性

uni-app(3.6.11)

Vue2 Vue3 Chrome Safari app-vue app-nvue Android iOS 鸿蒙
- - - - - -
微信小程序 支付宝小程序 抖音小程序 百度小程序 快手小程序 京东小程序 鸿蒙元服务 QQ小程序 飞书小程序 快应用-华为 快应用-联盟
- - - - - - - - - - -

yi-datetime-picker - 公历农历日期时间选择器

支持公历和农历的日期时间选择器组件,零依赖,轻量级,功能强大

Version License


🌟 核心特性

⭐ 零依赖

  • 不依赖 tyme4ts 或其他第三方库
  • 使用静态数据和纯 JavaScript 算法
  • 文件体积小,性能优异

🌞🌙 双历法支持

  • 公历选择:年/月/日/时/分/秒
  • 农历选择:年/月/日/时辰(含闰月、干支年)
  • 自由切换:一键切换公历/农历

🎨 UI精美

  • 完全模仿 uView Next 风格
  • 支持自定义主题色
  • 流畅的滚动动画

🔧 功能强大

  • ✅ 日期范围限制(minDate/maxDate)
  • ✅ 多种选择模式(datetime/date/time/year-month)
  • ✅ 自定义格式化(formatter)
  • ✅ 步进控制(minuteStep/hourStep)
  • ✅ 禁用/只读/加载状态
  • ✅ 确认前验证钩子

📱 跨平台

  • ✅ H5
  • ✅ 微信小程序
  • ✅ 支付宝小程序
  • ✅ App

📸 效果展示

公历选择

公历选择

农历选择

农历选择

多种模式

多种模式


📦 安装使用

方式一:下载组件

  1. 从插件市场下载 yi-datetime-picker 组件
  2. 复制到项目的 components 目录下

方式二:npm 安装(如果支持)

npm install yi-datetime-picker

🚀 快速开始

最简单的使用

<template>
  <view>
    <button @click="showPicker = true">选择日期时间</button>

    <yi-datetime-picker
      v-model="selectedTime"
      :show="showPicker"
      @confirm="handleConfirm"
      @cancel="showPicker = false"
    />
  </view>
</template>

<script setup>
import { ref } from 'vue'

const selectedTime = ref(Date.now())
const showPicker = ref(false)

const handleConfirm = (timestamp, formatted) => {
  console.log('时间戳:', timestamp)
  console.log('格式化:', formatted)
  showPicker.value = false
}
</script>

📖 详细文档

Props 属性

属性 类型 默认值 说明
show Boolean - 必填,是否显示选择器
modelValue (v-model) Number - 必填,时间戳(毫秒)
enableLunar Boolean true 是否启用农历
showSeconds Boolean false 是否显示秒
mode String 'datetime' 选择模式:datetime/date/time/year-month
returnFormat String 'timestamp' 返回格式:timestamp/date/iso
minDate Number - 最小日期时间戳
maxDate Number - 最大日期时间戳
minuteStep Number 1 分钟步进(1/5/10/15/30)
hourStep Number 1 小时步进(1/2/3/6)
disabled Boolean false 禁用状态
readonly Boolean false 只读状态
loading Boolean false 加载状态
formatter Function - 自定义格式化函数
beforeConfirm Function - 确认前钩子

更多属性请查看完整文档 → README.md


💡 使用场景

场景1:生日选择器(限制日期范围)

<yi-datetime-picker
  v-model="birthday"
  :show="visible"
  :min-date="new Date(1900, 0, 1).getTime()"
  :max-date="Date.now()"
  mode="date"
  title="选择生日"
/>

场景2:只选时间(闹钟设置)

<yi-datetime-picker
  v-model="alarmTime"
  :show="visible"
  mode="time"
  title="设置闹钟"
/>

场景3:农历选择(六爻起卦)

<template>
  <yi-datetime-picker
    ref="pickerRef"
    v-model="divineTime"
    :show="visible"
    default-type="lunar"
    enable-lunar
    @confirm="handleConfirm"
  />
</template>

<script setup>
const pickerRef = ref()

const handleConfirm = (timestamp, formatted) => {
  // 获取农历数据
  const lunarData = pickerRef.value.getLunarData()
  console.log('农历:', lunarData)
  // { type: 'lunar', year: 2024, month: 12, day: 20, hour: 0 }
}
</script>

场景4:自定义显示格式

<yi-datetime-picker
  :formatter="(type, value) => {
    if (type === 'year') {
      if (value === new Date().getFullYear()) return '今年'
      return `${value}年`
    }
    return value
  }"
/>

更多示例请查看 → 使用示例.md


🌙 农历功能说明

闰月支持

  • 自动识别闰月年份
  • 闰月显示为"闰X月"
  • 基于国家标准(GB/T 33661-2017)

干支年

  • 使用60甲子循环算法
  • 显示格式:2024(甲辰)

时辰对照

时辰 时间段
子时 23:00-01:00
丑时 01:00-03:00
寅时 03:00-05:00
... ...

数据准确性

  • ✅ 1900-2100年闰月数据100%准确
  • ✅ 基于 tyme4ts 官方数据生成
  • ✅ 符合国家标准

⚠️ 注意事项

农历转换

重要: 本组件不负责农历转公历的转换!

选择器只负责UI选择和数据返回,农历到公历的转换需要调用方处理。

推荐方案:

  1. 使用 tyme4ts 库(精确转换)
  2. 使用后端API(服务器端转换)
  3. 通过 ref.getLunarData() 获取农历数据

🔧 暴露方法(通过 ref 调用)

方法 返回值 说明
getLunarData() LunarData 获取当前选中的农历数据
getSolarData() SolarData 获取当前选中的公历数据
getCurrentType() 'solar' | 'lunar' 获取当前选择类型

📊 性能优势

对比项 传统组件 yi-datetime-picker
依赖库 tyme4ts (200KB+) ✅ 零依赖
文件大小 ✅ 小(减少80%)
加载速度 ✅ 快
农历支持 部分支持 ✅ 完整支持
闰月 可能缺失 ✅ 100%准确

🎯 适用项目

  • ✅ 六爻、八字等命理应用
  • ✅ 日历应用
  • ✅ 预约系统
  • ✅ 闹钟、提醒应用
  • ✅ 任何需要公历/农历选择的场景

📝 更新日志

v2.1.0 (2025-10-22)

  • ✅ 实现 minDate/maxDate 日期范围限制
  • ✅ 实现 formatter 自定义格式化
  • ✅ 实现 mode 多样化(date/time/year-month)
  • ✅ 修复 readonly 只读状态
  • ✅ 完善 showSeconds 秒列显示

v2.0.0 (2025-01-22)

  • ✅ 零依赖重构
  • ✅ 完整农历支持
  • ✅ 暴露方法

完整更新日志 → CHANGELOG.md


💬 问题反馈

如有问题或建议,欢迎提交 Issue 或联系作者。


📄 开源协议

MIT License


⭐ 如果觉得有用,请给个星标!

让更多开发者受益! 🙏

隐私、权限声明

1. 本插件需要申请的系统权限列表:

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

许可协议

MIT License

Copyright (c) 2025 yi-datetime-picker

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

暂无用户评论。