# 日期滑动选择器弹窗
根据指定的日期范围创建日期滑动选择器,展示在弹窗上。
> **说明:**
>
> 该组件从API Version 8开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
>
> 本模块功能依赖UI的执行上下文,不可在UI上下文不明确的地方使用,参见[UIContext](../apis/js-apis-arkui-UIContext.md#uicontext)说明。
>
> 从API version 10开始,可以通过使用[UIContext](../apis/js-apis-arkui-UIContext.md#uicontext)中的[showDatePickerDialog](../apis/js-apis-arkui-UIContext.md#showdatepickerdialog)来明确UI的执行上下文。
## DatePickerDialog.show
show(options?: DatePickerDialogOptions)
定义日期滑动选择器弹窗并弹出。
**DatePickerDialogOptions参数:**
| 参数名 | 参数类型 | 必填 | 参数描述 |
| -------- | -------- | -------- | -------- |
| start | Date | 否 | 设置选择器的起始日期。
默认值:Date('1970-1-1') |
| end | Date | 否 | 设置选择器的结束日期。
默认值:Date('2100-12-31') |
| selected | Date | 否 | 设置当前选中的日期。
默认值:当前系统日期 |
| lunar | boolean | 否 | 日期是否显示为农历。
默认值:false |
| showTime10+ | boolean | 否 | 是否展示时间项。
默认值:false |
| useMilitaryTime10+ | boolean | 否 | 展示时间是否为24小时制。
默认值:false
**说明:**当展示时间为12小时制时,上下午与小时无联动关系。 |
| disappearTextStyle10+ | [PickerTextStyle](ts-basic-components-datepicker.md#pickertextstyle10类型说明) | 否 | 设置所有选项中最上和最下两个选项的文本颜色、字号、字体粗细。
默认值:
{
color: '#ff182431',
font: {
size: '14fp',
weight: FontWeight.Regular
}
} |
| textStyle10+ | [PickerTextStyle](ts-basic-components-datepicker.md#pickertextstyle10类型说明) | 否 | 设置所有选项中除了最上、最下及选中项以外的文本颜色、字号、字体粗细。
默认值:
{
color: '#ff182431',
font: {
size: '16fp',
weight: FontWeight.Regular
}
} |
| selectedTextStyle10+ | [PickerTextStyle](ts-basic-components-datepicker.md#pickertextstyle10类型说明) | 否 | 设置选中项的文本颜色、字号、字体粗细。
默认值:
{
color: '#ff007dff',
font: {
size: '20vp',
weight: FontWeight.Medium
}
} |
| alignment10+ | [DialogAlignment](ts-methods-alert-dialog-box.md#dialogalignment枚举说明) | 否 | 弹窗在竖直方向上的对齐方式。
默认值:DialogAlignment.Default |
| offset10+ | [Offset](ts-types.md#offset) | 否 | 弹窗相对alignment所在位置的偏移量。
默认值:{ dx: 0 , dy: 0 } |
| maskRect10+| [Rectangle](ts-methods-alert-dialog-box.md#rectangle10类型说明) | 否 | 弹窗遮蔽层区域,在遮蔽层区域内的事件不透传,在遮蔽层区域外的事件透传。
默认值:{ x: 0, y: 0, width: '100%', height: '100%' } |
| onAccept(deprecated) | (value: [DatePickerResult](ts-basic-components-datepicker.md#DatePickerResult对象说明)) => void | 否 | 点击弹窗中的“确定”按钮时触发该回调。
**说明:**
从API version 8 开始支持,从 API version 10 开始废弃,建议使用onDateAccept。 |
| onCancel | () => void | 否 | 点击弹窗中的“取消”按钮时触发该回调。 |
| onChange(deprecated) | (value: [DatePickerResult](ts-basic-components-datepicker.md#DatePickerResult对象说明)) => void | 否 | 滑动弹窗中的滑动选择器使当前选中项改变时触发该回调。
**说明:**
从API version 8 开始支持,从 API version 10 开始废弃,建议使用onDateChange。 |
| onDateAccept10+ | (value: Date) => void | 否 | 点击弹窗中的“确定”按钮时触发该回调。
**说明:**
当showTime设置为true时,回调接口返回值value中时和分为选择器选择的时和分。否则,返回值value中时和分为系统时间的时和分。 |
| onDateChange10+ | (value: Date) => void | 否 | 滑动弹窗中的滑动选择器使当前选中项改变时触发该回调。
**说明:**
当showTime设置为true时,回调接口返回值value中时和分为选择器选择的时和分。否则,返回值value中时和分为系统时间的时和分。 |
**异常情形说明:**
| 异常情形 | 对应结果 |
| -------- | ------------------------------------------------------------ |
| 起始日期晚于结束日期,选中日期未设置 | 起始日期、结束日期和选中日期都为默认值 |
| 起始日期晚于结束日期,选中日期早于起始日期默认值 | 起始日期、结束日期都为默认值,选中日期为起始日期默认值 |
| 起始日期晚于结束日期,选中日期晚于结束日期默认值 | 起始日期、结束日期都为默认值,选中日期为结束日期默认值 |
| 起始日期晚于结束日期,选中日期在起始日期与结束日期默认值范围内 | 起始日期、结束日期都为默认值,选中日期为设置的值 |
| 选中日期早于起始日期 | 选中日期为起始日期 |
| 选中日期晚于结束日期 | 选中日期为结束日期 |
| 起始日期晚于当前系统日期,选中日期未设置 | 选中日期为起始日期 |
| 结束日期早于当前系统日期,选中日期未设置 | 选中日期为结束日期 |
| 日期格式不符合规范,如‘1999-13-32’ | 取默认值 |
| 起始日期或结束日期早于系统有效范围 | 起始日期或结束日期取系统有效范围最早日期 |
| 起始日期或结束日期晚于系统有效范围 | 起始日期或结束日期取系统有效范围最晚日期 |
系统日期范围:1900-1-31 ~ 2100-12-31
选中日期会在起始日期与结束日期异常处理完成后再进行异常情形判断处理
## 示例
```ts
// xxx.ets
@Entry
@Component
struct DatePickerDialogExample {
selectedDate: Date = new Date("2010-1-1")
build() {
Column() {
Button("DatePickerDialog")
.margin(20)
.onClick(() => {
DatePickerDialog.show({
start: new Date("2000-1-1"),
end: new Date("2100-12-31"),
selected: this.selectedDate,
showTime:true,
useMilitaryTime:false,
disappearTextStyle: {color: Color.Pink, font: {size: '22fp', weight: FontWeight.Bold}},
textStyle: {color: '#ff00ff00', font: {size: '18fp', weight: FontWeight.Normal}},
selectedTextStyle: {color: '#ff182431', font: {size: '14fp', weight: FontWeight.Regular}},
onDateAccept: (value: Date) => {
// 通过Date的setFullYear方法设置按下确定按钮时的日期,这样当弹窗再次弹出时显示选中的是上一次确定的日期
this.selectedDate = value
console.info("DatePickerDialog:onDateAccept()" + value.toString())
},
onCancel: () => {
console.info("DatePickerDialog:onCancel()")
},
onDateChange: (value: Date) => {
console.info("DatePickerDialog:onDateChange()" + value.toString())
}
})
})
Button("Lunar DatePickerDialog")
.margin(20)
.onClick(() => {
DatePickerDialog.show({
start: new Date("2000-1-1"),
end: new Date("2100-12-31"),
selected: this.selectedDate,
lunar: true,
disappearTextStyle: {color: Color.Pink, font: {size: '22fp', weight: FontWeight.Bold}},
textStyle: {color: '#ff00ff00', font: {size: '18fp', weight: FontWeight.Normal}},
selectedTextStyle: {color: '#ff182431', font: {size: '14fp', weight: FontWeight.Regular}},
onDateAccept: (value: Date) => {
this.selectedDate = value
console.info("DatePickerDialog:onDateAccept()" + value.toString())
},
onCancel: () => {
console.info("DatePickerDialog:onCancel()")
},
onDateChange: (value: Date) => {
console.info("DatePickerDialog:onDateChange()" + value.toString())
}
})
})
}.width('100%')
}
}
```
