1# Date Picker Dialog Box (DatePickerDialog) 2<!--Kit: ArkUI--> 3<!--Subsystem: ArkUI--> 4<!--Owner: @luoying_ace_admin--> 5<!--Designer: @weixin_52725220--> 6<!--Tester: @xiong0104--> 7<!--Adviser: @HelloCrease--> 8 9A date picker dialog box is a dialog box that allows users to select a date from the given range. 10 11> **NOTE** 12> 13> - The APIs of this module are supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version. 14> 15> - 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](../../../ui/arkts-global-interface.md). For details, see [UIContext](../arkts-apis-uicontext-uicontext.md). 16 17## DatePickerDialog 18 19### show<sup>(deprecated)</sup> 20 21static show(options?: DatePickerDialogOptions) 22 23Shows a date picker dialog box. 24 25> **NOTE** 26> 27> This API is deprecated since API version 18. You are advised to use [showDatePickerDialog](../arkts-apis-uicontext-uicontext.md#showdatepickerdialog) in [UIContext](../arkts-apis-uicontext-uicontext.md) instead. 28> 29> Since API version 10, you can use the [showDatePickerDialog](../arkts-apis-uicontext-uicontext.md#showdatepickerdialog) API in [UIContext](../arkts-apis-uicontext-uicontext.md), which ensures that the date picker dialog box is shown in the intended UI instance. 30 31**Atomic service API**: This API can be used in atomic services since API version 11. 32 33**System capability**: SystemCapability.ArkUI.ArkUI.Full 34 35**Device behavior differences**: On wearables, calling this API results in a runtime exception indicating that the API is undefined. On other devices, the API works correctly. 36 37**Parameters** 38 39| Name | Type | Mandatory| Description | 40| ------- | ----------------------------------------------------------- | ---- | -------------------------- | 41| options | [DatePickerDialogOptions](#datepickerdialogoptions) | No | Parameters of the date picker dialog box.| 42 43## DatePickerDialogOptions 44 45Defines the configuration options of the date picker dialog box. 46 47Inherited from [DatePickerOptions](ts-basic-components-datepicker.md#datepickeroptions). 48 49**System capability**: SystemCapability.ArkUI.ArkUI.Full 50 51| Name| Type| Read-Only| Optional| Description| 52| -------- | -------- | -------- | -------- | -------- | 53| lunar | boolean | No| Yes| Whether to display the lunar calendar. The value **true** means to display the lunar calendar, and **false** means the opposite.<br>Default value: **false**<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 54| showTime<sup>10+</sup> | boolean | No| Yes| Whether to display the time item. The value **true** means to display the time item, and **false** means the opposite.<br>Default value: **false**<br>With **showTime=true**, the **mode** parameter has no effect and the default three columns for year, month, and day are displayed.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 55| useMilitaryTime<sup>10+</sup> | boolean | No| Yes| 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.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 56| lunarSwitch<sup>10+</sup> | boolean | No| Yes| 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**<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 57| lunarSwitchStyle<sup>14+</sup> | [LunarSwitchStyle](#lunarswitchstyle14) | No| Yes| Style of the lunar calendar switch.<br>Default value: **{ selectedColor: $r('sys.color.ohos_id_color_text_primary_actived'), unselectedColor: $r('sys.color.ohos_id_color_switch_outline_off'), strokeColor: Color.White }**<br>**Atomic service API**: This API can be used in atomic services since API version 14.| 58| disappearTextStyle<sup>10+</sup> | [PickerTextStyle](ts-picker-common.md#pickertextstyle) | No| Yes| Text color, font size, and font weight of the items located two positions above or below the selected item.<br>Default value:<br>{<br>color: '#ff182431',<br>font: {<br>size: '14fp', <br>weight: FontWeight.Regular<br>}<br>}<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 59| textStyle<sup>10+</sup> | [PickerTextStyle](ts-picker-common.md#pickertextstyle) | No| Yes| Text color, font size, and font weight of the items located one position above or below the selected item.<br>Default value:<br>{<br>color: '#ff182431',<br>font: {<br>size: '16fp', <br>weight: FontWeight.Regular<br>}<br>}<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 60| selectedTextStyle<sup>10+</sup> | [PickerTextStyle](ts-picker-common.md#pickertextstyle) | No| Yes| Text color, font size, and font weight of the selected item.<br>Default value:<br>{<br>color: '#ff007dff',<br>font: {<br>size: '20fp', <br>weight: FontWeight.Medium<br>}<br>} <br>**Atomic service API**: This API can be used in atomic services since API version 11.| 61| acceptButtonStyle<sup>12+</sup> | [PickerDialogButtonStyle](ts-picker-common.md#pickerdialogbuttonstyle12) | No| Yes| Style of the accept button.<br>**NOTE**<br>1. In **acceptButtonStyle** and **cancelButtonStyle**, at most one **primary** field can be set to **true**. If both are set to **true**, the **primary** field will remain at the default value of **false**.<br>2. The default button height is 40 vp and remains fixed even in accessibility and large-font modes. In addition, even if the button style is set to [ROUNDED_RECTANGLE](ts-basic-components-button.md#buttontype), the displayed effect is still a capsule button ([Capsule](ts-basic-components-button.md#buttontype)).<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 62| cancelButtonStyle<sup>12+</sup> | [PickerDialogButtonStyle](ts-picker-common.md#pickerdialogbuttonstyle12) | No| Yes| Style of the cancel button.<br>**NOTE**<br>1. In **acceptButtonStyle** and **cancelButtonStyle**, at most one **primary** field can be set to **true**. If both are set to **true**, the **primary** field will remain at the default value of **false**.<br>2. The default button height is 40 vp and remains fixed even in accessibility and large-font modes. In addition, even if the button style is set to [ROUNDED_RECTANGLE](ts-basic-components-button.md#buttontype), the displayed effect is still a capsule button ([Capsule](ts-basic-components-button.md#buttontype)).<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 63| alignment<sup>10+</sup> | [DialogAlignment](ts-methods-alert-dialog-box.md#dialogalignment) | No | Yes | Alignment mode of the dialog box in the vertical direction.<br>Default value: **DialogAlignment.Default**<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 64| offset<sup>10+</sup> | [Offset](ts-types.md#offset) | No | Yes | Offset of the dialog box based on the **alignment** settings.<br>Default value: **{ dx: 0 , dy: 0 }**<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 65| maskRect<sup>10+</sup>| [Rectangle](ts-methods-alert-dialog-box.md#rectangle8) | No | Yes | 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%' }**<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 66| onAccept<sup>(deprecated)</sup> | (value: [DatePickerResult](ts-basic-components-datepicker.md#datepickerresult)) => void | No| Yes| 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.| 67| onCancel | [VoidCallback](ts-types.md#voidcallback12) | No| Yes| Callback invoked when the Cancel button in the dialog box is clicked.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 68| onChange<sup>(deprecated)</sup> | (value: [DatePickerResult](ts-basic-components-datepicker.md#datepickerresult)) => void | No| Yes| 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.| 69| onDateAccept<sup>10+</sup> | [Callback](ts-types.md#callback12)\<Date> | No| Yes| 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.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 70| onDateChange<sup>10+</sup> | [Callback](ts-types.md#callback12)\<Date> | No| Yes| 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.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 71| backgroundColor<sup>11+</sup> | [ResourceColor](ts-types.md#resourcecolor) | No| Yes| Backplane color of the dialog box.<br>Default value: **Color.Transparent**<br>**NOTE**<br>When **backgroundColor** is set to a non-transparent color, **backgroundBlurStyle** must be set to **BlurStyle.NONE**; otherwise, the color display may not meet the expected effect.<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 72| backgroundBlurStyle<sup>11+</sup> | [BlurStyle](ts-universal-attributes-background.md#blurstyle9) | No| Yes| Background blur style of the dialog box.<br>Default value: **BlurStyle.COMPONENT_ULTRA_THICK**<br>**NOTE**<br>Setting this parameter to **BlurStyle.NONE** disables the background blur. When **backgroundBlurStyle** is set to a value other than **NONE**, do not set **backgroundColor**. If you do, the color display may not produce the expected visual effect.<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 73| backgroundBlurStyleOptions<sup>19+</sup> | [BackgroundBlurStyleOptions](ts-universal-attributes-background.md#backgroundblurstyleoptions10) | No| Yes| Options for customizing the background blur style.<br>**Atomic service API**: This API can be used in atomic services since API version 19.| 74| backgroundEffect<sup>19+</sup> | [BackgroundEffectOptions](ts-universal-attributes-background.md#backgroundeffectoptions11) | No| Yes| Options for customizing the background effect.<br>**Atomic service API**: This API can be used in atomic services since API version 19.| 75| onDidAppear<sup>12+</sup> | [VoidCallback](ts-types.md#voidcallback12) | No| Yes| Event callback when the dialog box appears.<br>**NOTE**<br>1. The normal timing sequence is as follows: onWillAppear > onDidAppear > (onDateAccept/onCancel/onDateChange) > onWillDisappear > onDidDisappear.<br>2. You can set the callback event for changing the dialog box display effect in **onDidAppear**. The settings take effect next time the dialog box appears.<br>3. If the user closes the dialog box immediately after it appears, **onWillDisappear** is invoked before **onDidAppear**.<br>4. If the dialog box is closed before its entrance animation is finished, this callback is not invoked.<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 76| onDidDisappear<sup>12+</sup> | [VoidCallback](ts-types.md#voidcallback12) | No| Yes| Event callback when the dialog box disappears.<br>**NOTE**<br>1. The normal timing sequence is as follows: onWillAppear > onDidAppear > (onDateAccept/onCancel/onDateChange) > onWillDisappear > onDidDisappear.<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 77| onWillAppear<sup>12+</sup> | [VoidCallback](ts-types.md#voidcallback12) | No| Yes| Event callback when the dialog box is about to appear.<br>**NOTE**<br>1. The normal timing sequence is as follows: onWillAppear > onDidAppear > (onDateAccept/onCancel/onDateChange) > onWillDisappear > onDidDisappear.<br>2. You can set the callback event for changing the dialog box display effect in **onWillAppear**. The settings take effect next time the dialog box appears.<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 78| onWillDisappear<sup>12+</sup> | [VoidCallback](ts-types.md#voidcallback12) | No| Yes| Event callback when the dialog box is about to disappear.<br>**NOTE**<br>1. The normal timing sequence is as follows: onWillAppear > onDidAppear > (onDateAccept/onCancel/onDateChange) > onWillDisappear > onDidDisappear.<br>2. If the user closes the dialog box immediately after it appears, **onWillDisappear** is invoked before **onDidAppear**.<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 79| shadow<sup>12+</sup> | [ShadowOptions](ts-universal-attributes-image-effect.md#shadowoptions) \| [ShadowStyle](ts-universal-attributes-image-effect.md#shadowstyle10) | No | Yes | Shadow of the dialog box.<br> Default value on 2-in-1 devices: **ShadowStyle.OUTER_FLOATING_MD** when the dialog box is focused and **ShadowStyle.OUTER_FLOATING_SM** otherwise<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 80| dateTimeOptions<sup>12+</sup> | [DateTimeOptions](../../apis-localization-kit/js-apis-intl.md#datetimeoptionsdeprecated) | No| Yes| Whether to display a leading zero for the hours and minutes. Only the configuration of the **hour** and **minute** parameters is supported.<br>Default value:<br>**hour**: For the 24-hour format, the default value is **"2-digit"**, meaning the hour is displayed as a two-digit number. If the actual value is less than 10, a leading zero is added, displayed as "0X". For the 12-hour format, the default value is **"numeric"**, meaning no leading zero.<br>**minute**: The default value is **"2-digit"**, meaning the minute is displayed as a two-digit number. If the actual value is less than 10, a leading zero is added, displayed as "0X".<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 81| enableHoverMode<sup>14+</sup> | boolean | No | Yes | Whether to respond when the device is in semi-folded mode.<br>Default value: **false**. The value **true** means to respond when the device is in semi-folded mode, and **false** means the opposite.<br>**Atomic service API**: This API can be used in atomic services since API version 14.| 82| hoverModeArea<sup>14+</sup> | [HoverModeAreaType](ts-appendix-enums.md#hovermodeareatype14) | No | Yes | Display area of the dialog box when the device is in semi-folded mode.<br>Default value: **HoverModeAreaType.BOTTOM_SCREEN**<br>**Atomic service API**: This API can be used in atomic services since API version 14.| 83| enableHapticFeedback<sup>18+</sup> | boolean | No | Yes | Whether to enable haptic feedback.<br>The value **true** means to enable haptic feedback, and **false** means the opposite. The default value is **true**.<br>**Atomic service API**: This API can be used in atomic services since API version 18.<br>**NOTE**<br>To enable haptic feedback, you must declare the ohos.permission.VIBRATE permission under **requestPermissions** in the **module.json5** file of the project.<br>"requestPermissions": [{"name": "ohos.permission.VIBRATE"}]| 84| canLoop<sup>20+</sup> | boolean | No| Yes| Whether to enable cyclic scrolling.<br>Default value: **true**.<br>**NOTE**<br>**true**: Cyclic scrolling is enabled, where the year values increment or decrement with month cycling, and month values increment or decrement with day cycling.<br>**false**: Cyclic scrolling is disabled, preventing out-of-bounds scrolling in year, month, and day columns and cross-column value synchronization.<br>**Atomic service API**: This API can be used in atomic services since API version 20.| 85 86## LunarSwitchStyle<sup>14+</sup> 87 88Defines the style of the lunar calendar switch in the **DatePickerDialog** component. 89 90**Atomic service API**: This API can be used in atomic services since API version 14. 91 92**System capability**: SystemCapability.ArkUI.ArkUI.Full 93 94| Name | Type | Read-Only| Optional| Description | 95| ------ |-------------------|-----|----------|----------| 96| selectedColor| [ResourceColor](ts-types.md#resourcecolor) | No | Yes | Background color of the switch when it is on.<br>Default value: **$r('sys.color.ohos_id_color_text_primary_actived')**| 97| unselectedColor | [ResourceColor](ts-types.md#resourcecolor) | No | Yes | Border color of the switch when it is off.<br>Default value: **$r('sys.color.ohos_id_color_switch_outline_off')**| 98| strokeColor | [ResourceColor](ts-types.md#resourcecolor) | No | Yes | Color of the icon inside the switch.<br>Default value: **Color.White**| 99 100**Handling in the case of exceptions** 101 102| Exception | Result | 103| -------- | ------------------------------------------------------------ | 104| 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. | 105| 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. | 106| 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. | 107| 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.| 108| The selected date is earlier than the start date. | The selected date is set to the start date. | 109| The selected date is later than the end date. | The selected date is set to the end date. | 110| 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. | 111| 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. | 112| The set date is in invalid format, for example, **'1999-13-32'**. | The default value is used. | 113| 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. | 114| 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. | 115 116The valid date range is from 1900-1-31 to 2100-12-31. 117 118The exception detection and handling with the selected date comes after that with the start date and end date. 119 120## Example 121 122> **NOTE** 123> 124> For clarity in UI execution context, you are advised to use the [showDatePickerDialog](../arkts-apis-uicontext-uicontext.md#showdatepickerdialog) API in [UIContext](../arkts-apis-uicontext-uicontext.md). 125 126### Example 1: Setting the Display Time 127 128This example demonstrates how to set the display time using **showTime**, **useMilitaryTime**, and **dateTimeOptions**. 129 130```ts 131// xxx.ets 132@Entry 133@Component 134struct DatePickerDialogExample { 135 selectedDate: Date = new Date("2010-1-1"); 136 137 build() { 138 Column() { 139 Button("DatePickerDialog") 140 .margin(20) 141 .onClick(() => { 142 this.getUIContext().showDatePickerDialog({ 143 start: new Date("2000-1-1"), 144 end: new Date("2100-12-31"), 145 selected: this.selectedDate, 146 showTime: true, 147 useMilitaryTime: false, 148 dateTimeOptions: { hour: "numeric", minute: "2-digit" }, 149 onDateAccept: (value: Date) => { 150 // 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. 151 this.selectedDate = value; 152 console.info("DatePickerDialog:onDateAccept()" + value.toString()); 153 }, 154 onCancel: () => { 155 console.info("DatePickerDialog:onCancel()"); 156 }, 157 onDateChange: (value: Date) => { 158 console.info("DatePickerDialog:onDateChange()" + value.toString()); 159 }, 160 onDidAppear: () => { 161 console.info("DatePickerDialog:onDidAppear()"); 162 }, 163 onDidDisappear: () => { 164 console.info("DatePickerDialog:onDidDisappear()"); 165 }, 166 onWillAppear: () => { 167 console.info("DatePickerDialog:onWillAppear()"); 168 }, 169 onWillDisappear: () => { 170 console.info("DatePickerDialog:onWillDisappear()"); 171 } 172 }) 173 }) 174 }.width('100%') 175 } 176} 177``` 178 179 180 181### Example 2: Customizing the Style 182 183In this example, **disappearTextStyle**, **textStyle**, **selectedTextStyle**, **acceptButtonStyle**, and **cancelButtonStyle** are configured to customize the text and button style. 184 185```ts 186// xxx.ets 187@Entry 188@Component 189struct DatePickerDialogExample { 190 selectedDate: Date = new Date("2010-1-1"); 191 192 build() { 193 Column() { 194 Button("DatePickerDialog") 195 .margin(20) 196 .onClick(() => { 197 this.getUIContext().showDatePickerDialog({ 198 start: new Date("2000-1-1"), 199 end: new Date("2100-12-31"), 200 selected: this.selectedDate, 201 disappearTextStyle: { color: '#297bec', font: { size: '20fp', weight: FontWeight.Bold } }, 202 textStyle: { color: Color.Black, font: { size: '18fp', weight: FontWeight.Normal } }, 203 selectedTextStyle: { color: Color.Blue, font: { size: '26fp', weight: FontWeight.Regular } }, 204 acceptButtonStyle: { 205 type: ButtonType.Normal, 206 style: ButtonStyleMode.NORMAL, 207 role: ButtonRole.NORMAL, 208 fontColor: 'rgb(81, 81, 216)', 209 fontSize: '26fp', 210 fontWeight: FontWeight.Bolder, 211 fontStyle: FontStyle.Normal, 212 fontFamily: 'sans-serif', 213 backgroundColor: '#A6ACAF', 214 borderRadius: 20 215 }, 216 cancelButtonStyle: { 217 type: ButtonType.Normal, 218 style: ButtonStyleMode.NORMAL, 219 role: ButtonRole.NORMAL, 220 fontColor: Color.Blue, 221 fontSize: '16fp', 222 fontWeight: FontWeight.Normal, 223 fontStyle: FontStyle.Italic, 224 fontFamily: 'sans-serif', 225 backgroundColor: '#50182431', 226 borderRadius: 10 227 }, 228 onDateAccept: (value: Date) => { 229 // 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. 230 this.selectedDate = value; 231 console.info("DatePickerDialog:onDateAccept()" + value.toString()); 232 }, 233 onCancel: () => { 234 console.info("DatePickerDialog:onCancel()"); 235 }, 236 onDateChange: (value: Date) => { 237 console.info("DatePickerDialog:onDateChange()" + value.toString()); 238 }, 239 onDidAppear: () => { 240 console.info("DatePickerDialog:onDidAppear()"); 241 }, 242 onDidDisappear: () => { 243 console.info("DatePickerDialog:onDidDisappear()"); 244 }, 245 onWillAppear: () => { 246 console.info("DatePickerDialog:onWillAppear()"); 247 }, 248 onWillDisappear: () => { 249 console.info("DatePickerDialog:onWillDisappear()"); 250 } 251 }); 252 }) 253 }.width('100%') 254 } 255} 256``` 257 258 259 260> **NOTE** 261> 262> To implement a fully customized date picker dialog box, create a [custom dialog box](ts-methods-custom-dialog-box.md) and then implement the [DatePicker](ts-basic-components-datepicker.md) component. 263 264### Example 3: Configuring a Dialog Box in the Hover State 265 266This example demonstrates how to set the layout area of a dialog box when the device is in semi-folded mode. 267 268```ts 269@Entry 270@Component 271struct DatePickerDialogExample { 272 selectedDate: Date = new Date("2010-1-1"); 273 274 build() { 275 Column() { 276 Button("DatePickerDialog") 277 .margin(20) 278 .onClick(() => { 279 this.getUIContext().showDatePickerDialog({ 280 start: new Date("2000-1-1"), 281 end: new Date("2100-12-31"), 282 selected: this.selectedDate, 283 showTime: true, 284 useMilitaryTime: false, 285 disappearTextStyle: { color: Color.Pink, font: { size: '22fp', weight: FontWeight.Bold }}, 286 textStyle: { color: '#ff00ff00', font: { size: '18fp', weight: FontWeight.Normal }}, 287 selectedTextStyle: { color: '#ff182431', font: { size: '14fp', weight: FontWeight.Regular }}, 288 onDateAccept: (value: Date) => { 289 // 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. 290 this.selectedDate = value; 291 console.info("DatePickerDialog:onDateAccept()" + value.toString()); 292 }, 293 onCancel: () => { 294 console.info("DatePickerDialog:onCancel()"); 295 }, 296 onDateChange: (value: Date) => { 297 console.info("DatePickerDialog:onDateChange()" + value.toString()); 298 }, 299 onDidAppear: () => { 300 console.info("DatePickerDialog:onDidAppear()"); 301 }, 302 onDidDisappear: () => { 303 console.info("DatePickerDialog:onDidDisappear()"); 304 }, 305 onWillAppear: () => { 306 console.info("DatePickerDialog:onWillAppear()"); 307 }, 308 onWillDisappear: () => { 309 console.info("DatePickerDialog:onWillDisappear()"); 310 }, 311 enableHoverMode: true, 312 hoverModeArea: HoverModeAreaType.TOP_SCREEN 313 }); 314 }) 315 }.width('100%') 316 } 317} 318``` 319 320 321 322### Example 4: Setting the Dialog Box Position 323 324This example demonstrates how to set the position of a dialog box using **alignment** and **offset**. 325 326```ts 327// xxx.ets 328@Entry 329@Component 330struct DatePickerDialogExample { 331 selectedDate: Date = new Date("2010-1-1"); 332 333 build() { 334 Column() { 335 Button("DatePickerDialog") 336 .margin(20) 337 .onClick(() => { 338 this.getUIContext().showDatePickerDialog({ 339 start: new Date("2000-1-1"), 340 end: new Date("2100-12-31"), 341 selected: this.selectedDate, 342 alignment: DialogAlignment.Center, 343 offset: { dx: 20, dy: 0 }, 344 onDateAccept: (value: Date) => { 345 // 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. 346 this.selectedDate = value; 347 console.info("DatePickerDialog:onDateAccept()" + value.toString()); 348 } 349 }); 350 }) 351 }.width('100%') 352 } 353} 354``` 355 356 357 358### Example 5: Setting the Mask Area 359 360This example demonstrates how to set the mask area using **maskRect**. 361 362```ts 363// xxx.ets 364@Entry 365@Component 366struct DatePickerDialogExample { 367 selectedDate: Date = new Date("2010-1-1"); 368 369 build() { 370 Column() { 371 Button("DatePickerDialog") 372 .margin(20) 373 .onClick(() => { 374 this.getUIContext().showDatePickerDialog({ 375 start: new Date("2000-1-1"), 376 end: new Date("2100-12-31"), 377 selected: this.selectedDate, 378 maskRect: { 379 x: 30, 380 y: 60, 381 width: '100%', 382 height: '60%' 383 }, 384 onDateAccept: (value: Date) => { 385 // 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. 386 this.selectedDate = value; 387 console.info("DatePickerDialog:onDateAccept()" + value.toString()); 388 } 389 }); 390 }) 391 }.width('100%') 392 } 393} 394``` 395 396 397 398### Example 6: Setting the Background 399 400This example demonstrates how to set the dialog box background using **backgroundColor**, **backgroundBlurStyle**, and **shadow**. 401 402```ts 403// xxx.ets 404@Entry 405@Component 406struct DatePickerDialogExample { 407 selectedDate: Date = new Date("2010-1-1"); 408 409 build() { 410 Column() { 411 Button("DatePickerDialog") 412 .margin(20) 413 .onClick(() => { 414 this.getUIContext().showDatePickerDialog({ 415 start: new Date("2000-1-1"), 416 end: new Date("2100-12-31"), 417 selected: this.selectedDate, 418 backgroundColor: 'rgb(204, 226, 251)', 419 backgroundBlurStyle: BlurStyle.NONE, 420 shadow: ShadowStyle.OUTER_FLOATING_SM, 421 onDateAccept: (value: Date) => { 422 // 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. 423 this.selectedDate = value; 424 console.info("DatePickerDialog:onDateAccept()" + value.toString()); 425 } 426 }); 427 }) 428 }.width('100%') 429 } 430} 431``` 432 433 434 435### Example 7: Switching Between Gregorian and Lunar Calendars 436 437This example demonstrates how to set the date picker dialog box to display either the Gregorian (solar) calendar or the lunar calendar using **lunar** and **lunarSwitch**. 438 439```ts 440// xxx.ets 441@Entry 442@Component 443struct DatePickerDialogExample { 444 selectedDate: Date = new Date("2010-1-1"); 445 446 build() { 447 Column() { 448 Button("DatePickerDialog") 449 .margin(20) 450 .onClick(() => { 451 this.getUIContext().showDatePickerDialog({ 452 start: new Date("2000-1-1"), 453 end: new Date("2100-12-31"), 454 selected: this.selectedDate, 455 lunar: false, 456 onDateAccept: (value: Date) => { 457 // 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. 458 this.selectedDate = value; 459 console.info("DatePickerDialog:onDateAccept()" + value.toString()); 460 } 461 }); 462 }) 463 464 Button("Lunar DatePickerDialog") 465 .margin(20) 466 .onClick(() => { 467 this.getUIContext().showDatePickerDialog({ 468 start: new Date("2000-1-1"), 469 end: new Date("2100-12-31"), 470 selected: this.selectedDate, 471 lunar: true, 472 lunarSwitch: true, 473 onDateAccept: (value: Date) => { 474 this.selectedDate = value; 475 console.info("DatePickerDialog:onDateAccept()" + value.toString()); 476 } 477 }); 478 }) 479 }.width('100%') 480 } 481} 482``` 483 484 485 486### Example 8: Setting Display of Month and Day Columns 487 488This example demonstrates how to configure the **mode** parameter to display only the month and day columns in the date picker dialog box. 489 490```ts 491// xxx.ets 492@Entry 493@Component 494struct DatePickerDialogExample { 495 selectedDate: Date = new Date("2010-1-1"); 496 497 build() { 498 Column() { 499 Button("DatePickerDialog") 500 .margin(20) 501 .onClick(() => { 502 this.getUIContext().showDatePickerDialog({ 503 start: new Date("2000-1-1"), 504 end: new Date("2100-12-31"), 505 selected: this.selectedDate, 506 mode: DatePickerMode.MONTH_AND_DAY, 507 onDateAccept: (value: Date) => { 508 // 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. 509 this.selectedDate = value; 510 console.info("DatePickerDialog:onDateAccept()" + value.toString()); 511 } 512 }); 513 }) 514 }.width('100%') 515 } 516} 517``` 518 519 520 521### Example 9: Setting Cyclic Scrolling 522 523This example demonstrates how to set whether to enable cyclic scrolling using **canLoop**. 524 525```ts 526// xxx.ets 527@Entry 528@Component 529struct DatePickerDialogExample { 530 @State isLoop: boolean = true; 531 selectedDate: Date = new Date("2010-1-1"); 532 533 build() { 534 Column() { 535 Button("DatePickerDialog") 536 .margin(20) 537 .onClick(() => { 538 this.getUIContext().showDatePickerDialog({ 539 start: new Date("2000-1-1"), 540 end: new Date("2100-12-31"), 541 selected: this.selectedDate, 542 canLoop: this.isLoop, 543 onDateAccept: (value: Date) => { 544 // 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. 545 this.selectedDate = value; 546 console.info("DatePickerDialog:onDateAccept()" + value.toString()); 547 } 548 }); 549 }) 550 551 Row() { 552 Text('Cyclic scrolling').fontSize(20) 553 Toggle({ type: ToggleType.Switch, isOn: true }) 554 .onChange((isOn: boolean) => { 555 this.isLoop = isOn; 556 }) 557 }.position({ x: '60%', y: '40%' }) 558 }.width('100%') 559 } 560} 561``` 562 563 564 565### Example 10: Customizing the Background Blur Effect 566 567This example demonstrates how to customize the background blur effect by configuring [backgroundBlurStyleOptions](#datepickerdialogoptions). This functionality is supported since API version 19. 568 569```ts 570@Entry 571@Component 572struct DatePickerDialogExample { 573 selectedDate: Date = new Date("2010-1-1"); 574 575 build() { 576 Stack({ alignContent: Alignment.Top }) { 577 Image($r('app.media.bg')) 578 Column() { 579 Button("DatePickerDialog") 580 .margin(20) 581 .onClick(() => { 582 this.getUIContext().showDatePickerDialog({ 583 start: new Date("2000-1-1"), 584 end: new Date("2100-12-31"), 585 selected: this.selectedDate, 586 backgroundColor: undefined, 587 backgroundBlurStyle: BlurStyle.Thin, 588 backgroundBlurStyleOptions: { 589 colorMode: ThemeColorMode.LIGHT, 590 adaptiveColor: AdaptiveColor.AVERAGE, 591 scale: 1, 592 blurOptions: { grayscale: [20, 20] }, 593 }, 594 }); 595 }) 596 }.width('100%') 597 } 598 } 599} 600``` 601 602 603 604### Example 11: Customizing the Background Effect 605 606This example demonstrates how to customize the background effect by configuring [backgroundEffect](#datepickerdialogoptions). This functionality is supported since API version 19. 607 608```ts 609@Entry 610@Component 611struct DatePickerDialogExample { 612 selectedDate: Date = new Date("2010-1-1"); 613 614 build() { 615 Stack({ alignContent: Alignment.Top }) { 616 Image($r('app.media.bg')) 617 Column() { 618 Button("DatePickerDialog") 619 .margin(20) 620 .onClick(() => { 621 this.getUIContext().showDatePickerDialog({ 622 start: new Date("2000-1-1"), 623 end: new Date("2100-12-31"), 624 selected: this.selectedDate, 625 backgroundColor: undefined, 626 backgroundBlurStyle: BlurStyle.Thin, 627 backgroundEffect: { 628 radius: 60, 629 saturation: 0, 630 brightness: 1, 631 color: Color.White, 632 blurOptions: { grayscale: [20, 20] } 633 }, 634 }); 635 }) 636 }.width('100%') 637 } 638 } 639} 640``` 641 642 643