• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Date Picker Dialog Box (DatePickerDialog)
2
3A date picker dialog box is a dialog box that allows users to select a date from the given range.
4
5>  **NOTE**
6>
7> The APIs of this module are supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.
8>
9> The functionality of this module depends on UI context. This means that the APIs of this module cannot be used where the UI context is unclear. For details, see [UIContext](../apis/js-apis-arkui-UIContext.md#uicontext).
10>
11> Since API version 10, you can use the [showDatePickerDialog](../apis/js-apis-arkui-UIContext.md#showdatepickerdialog) API in [UIContext](../apis/js-apis-arkui-UIContext.md#uicontext) to obtain the UI context.
12
13## DatePickerDialog.show
14
15show(options?: DatePickerDialogOptions)
16
17Shows a date picker dialog box.
18
19**System capability**: SystemCapability.ArkUI.ArkUI.Full
20
21**Parameters**
22
23| Name | Type                                                       | Mandatory| Description                      |
24| ------- | ----------------------------------------------------------- | ---- | -------------------------- |
25| options | [DatePickerDialogOptions](#datepickerdialogoptions) | No  | Parameters of the date picker dialog box.|
26
27## DatePickerDialogOptions
28
29Inherited from [DatePickerOptions](ts-basic-components-datepicker.md#datepickeroptions).
30
31| Name| Type| Mandatory| Description|
32| -------- | -------- | -------- | -------- |
33| lunar | boolean | No| Whether to display the lunar calendar. The value **true** means to display the lunar calendar, and **false** means the opposite.<br>Default value: **false**|
34| showTime<sup>10+</sup> | boolean | No| Whether to display the time item. The value **true** means to display the time item, and **false** means the opposite.<br>Default value: **false**|
35| useMilitaryTime<sup>10+</sup> | boolean | No| Whether to display time in 24-hour format. The value **true** means to display time in 24-hour format, and **false** means the opposite.<br>Default value: **false**<br>**NOTE**<br>When the display time is in 12-hour format, the AM/PM zone does not change depending on the hour portion.|
36| lunarSwitch<sup>10+</sup> | boolean | No| Whether to display the lunar calendar switch. The value **true** means to display the lunar calendar switch, and **false** means the opposite.<br>Default value: **false**|
37| disappearTextStyle<sup>10+</sup> | [PickerTextStyle](ts-basic-components-datepicker.md#pickertextstyle10) | No| Font color, font size, and font width for the top and bottom items.<br>Default value:<br>{<br>color: '#ff182431',<br>font: {<br>size: '14fp', <br>weight: FontWeight.Regular<br>}<br>} |
38| textStyle<sup>10+</sup> | [PickerTextStyle](ts-basic-components-datepicker.md#pickertextstyle10) | No| Font color, font size, and font width of all items except the top, bottom, and selected items.<br>Default value:<br>{<br>color: '#ff182431',<br>font: {<br>size: '16fp', <br>weight: FontWeight.Regular<br>}<br>} |
39| selectedTextStyle<sup>10+</sup> | [PickerTextStyle](ts-basic-components-datepicker.md#pickertextstyle10) | No| Font color, font size, and font width of the selected item.<br>Default value:<br>{<br>color: '#ff007dff',<br>font: {<br>size: '20vp', <br>weight: FontWeight.Medium<br>}<br>} |
40| alignment<sup>10+</sup>  | [DialogAlignment](ts-methods-alert-dialog-box.md#dialogalignment) | No  | Alignment mode of the dialog box in the vertical direction.<br>Default value: **DialogAlignment.Default**|
41| offset<sup>10+</sup>     | [Offset](ts-types.md#offset) | No    | Offset of the dialog box based on the **alignment** settings.<br>Default value: **{ dx: 0 , dy: 0 }**|
42| maskRect<sup>10+</sup>| [Rectangle](ts-methods-alert-dialog-box.md#rectangle8) | No    | Mask area of the dialog box. Events outside the mask area are transparently transmitted, and events within the mask area are not.<br>Default value: **{ x: 0, y: 0, width: '100%', height: '100%' }**|
43| onAccept<sup>(deprecated)</sup> | (value: [DatePickerResult](ts-basic-components-datepicker.md#datepickerresult)) => void | No| Callback invoked when the OK button in the dialog box is clicked.<br>**NOTE**<br>This API is supported since API version 8 and deprecated since API version 10. You are advised to use **onDateAccept** instead.|
44| onCancel | () => void | No| Callback invoked when the Cancel button in the dialog box is clicked.|
45| onChange<sup>(deprecated)</sup> | (value: [DatePickerResult](ts-basic-components-datepicker.md#datepickerresult)) => void | No| Callback invoked when the selected item in the picker changes.<br>**NOTE**<br>This API is supported since API version 8 and deprecated since API version 10. You are advised to use **onDateChange** instead.|
46| onDateAccept<sup>10+</sup> | (value:  Date) => void | No| Callback invoked when the OK button in the dialog box is clicked.<br>**NOTE**<br>When **showTime** is set to **true**, the hour and minute in the value returned by the callback are the hour and minute selected in the picker. Otherwise, the hour and minute are the hour and minute of the system time.|
47| onDateChange<sup>10+</sup> | (value:  Date) => void | No| Callback invoked when the selected item in the picker changes.<br>**NOTE**<br>When **showTime** is set to **true**, the hour and minute in the value returned by the callback are the hour and minute selected in the picker. Otherwise, the hour and minute are the hour and minute of the system time.|
48| backgroundColor<sup>11+</sup> | [ResourceColor](ts-types.md#resourcecolor)  | No| Backplane color of the dialog box.<br>Default value: **Color.Transparent**|
49| backgroundBlurStyle<sup>11+</sup> | [BlurStyle](ts-appendix-enums.md#blurstyle9) | No| Background blur style of the dialog box.<br>Default value: **BlurStyle.COMPONENT_ULTRA_THICK**|
50
51**Handling in the case of exceptions**
52
53| Exception  | Result |
54| -------- |  ------------------------------------------------------------ |
55| The start date is later than the end date, and the selected date is not set.   | The start date, end date, and selected date are set to the default values. |
56| The start date is later than the end date, and the selected date is earlier than the default start date.   | The start date and end date are set to the default values, and the selected date is set to the default start date. |
57| The start date is later than the end date, and the selected date is later than the default end date.   | The start date and end date are set to the default values, and the selected date is set to the default end date. |
58| The start date is later than the end date, and the selected date is within the range of the default start date and end date.   | The start date and end date are set to the default values, and the selected date is set to the specified value.|
59| The selected date is earlier than the start date.   | The selected date is set to the start date. |
60| The selected date is later than the end date.   | The selected date is set to the end date. |
61| The start date is later than the current system date, and the selected date is not set.   | The selected date is set to the start date. |
62| The end date is earlier than the current system date, and the selected date is not set.   | The selected date is set to the end date. |
63| The set date is in invalid format, for example, **'1999-13-32'**.  | The default value is used. |
64| The start date or end date is earlier than the valid date range.   | The start date or end date is set to the earliest date in the valid date range. |
65| The start date or end date is later than the valid date range.   | The start date or end date is set to the latest date in the valid date range. |
66
67The valid date range is from 1900-1-31 to 2100-12-31.
68
69The exception detection and handling with the selected date comes after that with the start date and end date.
70
71## Example
72
73```ts
74// xxx.ets
75@Entry
76@Component
77struct DatePickerDialogExample {
78  selectedDate: Date = new Date("2010-1-1")
79
80  build() {
81    Column() {
82      Button("DatePickerDialog")
83        .margin(20)
84        .onClick(() => {
85          DatePickerDialog.show({
86            start: new Date("2000-1-1"),
87            end: new Date("2100-12-31"),
88            selected: this.selectedDate,
89            showTime:true,
90            useMilitaryTime:false,
91            disappearTextStyle: {color: Color.Pink, font: {size: '22fp', weight: FontWeight.Bold}},
92            textStyle: {color: '#ff00ff00', font: {size: '18fp', weight: FontWeight.Normal}},
93            selectedTextStyle: {color: '#ff182431', font: {size: '14fp', weight: FontWeight.Regular}},
94            onDateAccept: (value: Date) => {
95              // Use the setFullYear method to set the date when the OK button is touched. In this way, when the date picker dialog box is displayed again, the selected date is the date last confirmed.
96              this.selectedDate = value
97              console.info("DatePickerDialog:onDateAccept()" + value.toString())
98            },
99            onCancel: () => {
100              console.info("DatePickerDialog:onCancel()")
101            },
102            onDateChange: (value: Date) => {
103              console.info("DatePickerDialog:onDateChange()" + value.toString())
104            }
105          })
106        })
107
108      Button("Lunar DatePickerDialog")
109        .margin(20)
110        .onClick(() => {
111          DatePickerDialog.show({
112            start: new Date("2000-1-1"),
113            end: new Date("2100-12-31"),
114            selected: this.selectedDate,
115            lunar: true,
116            disappearTextStyle: {color: Color.Pink, font: {size: '22fp', weight: FontWeight.Bold}},
117            textStyle: {color: '#ff00ff00', font: {size: '18fp', weight: FontWeight.Normal}},
118            selectedTextStyle: {color: '#ff182431', font: {size: '14fp', weight: FontWeight.Regular}},
119            onDateAccept: (value: Date) => {
120              this.selectedDate = value
121              console.info("DatePickerDialog:onDateAccept()" + value.toString())
122            },
123            onCancel: () => {
124              console.info("DatePickerDialog:onCancel()")
125            },
126            onDateChange: (value: Date) => {
127              console.info("DatePickerDialog:onDateChange()" + value.toString())
128            }
129          })
130        })
131    }.width('100%')
132  }
133}
134```
135
136![DataPickerDialog](figures/DatePickerDialogApi10.gif)
137