Skip to content

DateTimePicker 日期时间选择

介绍

日期时间选择器,用于选择年、月、日、时、分、秒,通常与弹出层组件配合使用。

引入

ts
import { IBestDateTimePicker, IBestDateTimePickerResult } from "@ibestservices/ibest-ui-v2";

代码演示

基础用法

基础用法

TIP

通过 minDate maxDate 属性可控制日期范围。
• 默认最小日期为十年前;
• 默认最大日期为十年后。

点我查看代码
ts
@Entry
@ComponentV2
struct DemoPage {
  @Local selectValue: string[] = ["2025", "04", "13", "20", "12", "50"]
  build() {
    Column(){
      IBestDateTimePicker({
        title: "选择日期时间",
        value: this.selectValue!!,
        onConfirm: value => {
          IBestToast.show(value.year + '-' + value.month + '-' + value.day + ' ' + value.hour + ':' + value.minute + ':' + value.second)
        }
      })
    }
  }
}

显示单位

显示单位

点我查看代码
ts
@Entry
@ComponentV2
struct DemoPage {
  @Local selectValue: string[] = []
  build() {
    Column(){
      IBestDateTimePicker({
        title: "选择日期",
        showUnit: true,
        value: this.selectValue!!,
        onConfirm: (value: IBestDateTimePickerResult) => {
          IBestToast.show(value.year + '-' + value.month + '-' + value.day + ' ' + value.hour + ':' + value.minute + ':' + value.second)
        }
      })
    }
  }
}

API

@Props

参数说明类型默认值
value当前选中的日期, 支持双向绑定string[][]
minDate最小可选日期Date十年前
maxDate最大可选日期Date十年后
showUnit是否显示单位booleanfalse
showSecond是否显示秒booleantrue
title标题ResourceStr''
itemHeight单项高度number | string44
visibleItemCount可见选项数量number6
showToolBar是否显示顶部栏booleantrue
confirmText确认按钮文字ResourceStr确定
cancelText取消按钮文字ResourceStr取消
groupId分组id, 通常在配合PickerGroup组件使用时传入, 可实现多个DateTimePicker联动string | number''
optionFontSize选项字体大小number | string16

Events

事件名说明回调参数
onChange某一列选项变更后触发value: IBestDateTimePickerResult
onConfirm点击确定按钮时触发value: IBestDateTimePickerResult
onCancel点击取消按钮时触发-

IBestDateTimePickerResult 数据结构

参数说明类型
year选中的年份string
month选中的月份string
day选中的日期string
hour选中的小时string
minute选中的分钟string
second选中的秒string