1# Time Picker Dialog Box (TimePickerDialog) 2<!--Kit: ArkUI--> 3<!--Subsystem: ArkUI--> 4<!--Owner: @luoying_ace_admin--> 5<!--Designer: @weixin_52725220--> 6<!--Tester: @xiong0104--> 7<!--Adviser: @HelloCrease--> 8 9A time picker dialog box is a dialog box that allows users to select a time from the 24-hour range through scrolling. 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## TimePickerDialog 18 19### show<sup>(deprecated)</sup> 20 21static show(options?: TimePickerDialogOptions) 22 23Shows a time picker dialog box. 24 25> **NOTE** 26> 27> This API is deprecated since API version 18. You are advised to use [showTimePickerDialog](../arkts-apis-uicontext-uicontext.md#showtimepickerdialog) in [UIContext](../arkts-apis-uicontext-uicontext.md) instead. 28> 29> Since API version 10, you can use the [showTimePickerDialog](../arkts-apis-uicontext-uicontext.md#showtimepickerdialog) API in [UIContext](../arkts-apis-uicontext-uicontext.md), which ensures that the time 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 | [TimePickerDialogOptions](#timepickerdialogoptions) | No | Parameters of the time picker dialog box.| 42 43## TimePickerDialogOptions 44 45Defines the configuration options of the time picker dialog box. 46 47Inherited from [TimePickerOptions](ts-basic-components-timepicker.md#timepickeroptions). 48 49**System capability**: SystemCapability.ArkUI.ArkUI.Full 50 51| Name| Type| Read-Only| Optional| Description| 52| -------- | -------- | -------- | -------- | -------- | 53| useMilitaryTime | boolean | No| Yes| Whether to display time in 24-hour format. The 12-hour format is used by default.<br>Default value: **false**<br>**NOTE**<br>When in the 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.| 54| 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.| 55| 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.| 56| selectedTextStyle<sup>10+</sup> | [PickerTextStyle](ts-picker-common.md#pickertextstyle) | No| Yes| Font 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.| 57| 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.| 58| 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.| 59| 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.| 60| offset<sup>10+</sup> | [Offset](ts-types.md#offset) | No | Yes | Offset of the dialog box relative to the alignment position.<br>Default value: **{ dx: 0 , dy: 0 }**<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 61| 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.| 62| onAccept | (value: [TimePickerResult](ts-basic-components-timepicker.md#timepickerresult)) => void | No| Yes| Callback invoked when the OK button in the dialog box is clicked.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 63| onCancel | () => void | 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.| 64| onChange | (value: [TimePickerResult](ts-basic-components-timepicker.md#timepickerresult)) => void | No| Yes| Triggered when the text picker in the dialog box snaps to the selected item.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 65| 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.| 66| 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.| 67| 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.| 68| 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.| 69| onDidAppear<sup>12+</sup> | () => void | No| Yes| Event callback when the dialog box appears.<br>**NOTE**<br>1. The normal timing sequence is as follows: onWillAppear > onDidAppear > (onAccept/onCancel/onChange) > 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.| 70| onDidDisappear<sup>12+</sup> | () => void | No| Yes| Event callback when the dialog box disappears.<br>**NOTE**<br>1. The normal timing sequence is as follows: onWillAppear > onDidAppear > (onAccept/onCancel/onChange) > onWillDisappear > onDidDisappear.<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 71| onWillAppear<sup>12+</sup> | () => void | 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 > (onAccept/onCancel/onChange) > 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.| 72| onWillDisappear<sup>12+</sup> | () => void | 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 > (onAccept/onCancel/onChange) > 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.| 73| 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.| 74| 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. Currently 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.| 75| enableHoverMode<sup>14+</sup> | boolean | No | Yes | Whether to enable the hover mode.<br>Default value: **false**, meaning not to enable the hover mode.<br>**Atomic service API**: This API can be used in atomic services since API version 14.| 76| hoverModeArea<sup>14+</sup> | [HoverModeAreaType](ts-appendix-enums.md#hovermodeareatype14) | No | Yes | Display area of the dialog box in hover mode.<br>Default value: **HoverModeAreaType.BOTTOM_SCREEN**<br>**Atomic service API**: This API can be used in atomic services since API version 14.| 77| onEnterSelectedArea<sup>18+</sup> | Callback\<[TimePickerResult](ts-basic-components-timepicker.md#timepickerresult)> | No | Yes | Represents the callback triggered during the scrolling of the text picker when an item enters the divider area. Compared to the **onChange** event, this event is triggered earlier, specifically when the scroll distance of the current column exceeds half the height of the selected item, which indicates that the item has entered the divider area.<br>**NOTE**<br>When **enableCascade** is set to **true**, using this callback is not recommended due to the interdependent relationship between the AM/PM and hour columns. This callback indicates the moment an option enters the divider area during scrolling, and only the value of the currently scrolled column will change. The values of other non-scrolled columns will remain unchanged.<br>**Atomic service API**: This API can be used in atomic services since API version 18.| 78| enableCascade<sup>18+</sup> | boolean | No | Yes | Whether the AM/PM indicator automatically switches based on the hour in 12-hour format.<br>Default value: **false**. The value **true** means that the AM/PM indicator automatically switches based on the hour in 12-hour format, and **false** means the opposite.<br>**Atomic service API**: This API can be used in atomic services since API version 18.| 79| 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"}]| 80 81**Handling in the case of exceptions** 82 83| Exception | Result | 84| -------- | ------------------------------------------------------------ | 85| The start time is later than the end time. | Both start time and end time are set to their default values. | 86| The selected time is earlier than the start time. | The selected time is set to the start time. | 87| The selected time is later than the end time. | The selected time is set to the end time. | 88| The start time is later than the current system time, and the selected time is not set. | The selected time is set to the start time.| 89| The end time is earlier than the current system time, and the selected time is not set. | The selected time is set to the end time. | 90| The time format is invalid, such as **'01:61:61'**. | The default value is used. | 91 92## Example 93 94> **NOTE** 95> 96> For clarity in UI execution context, you are advised to use the [showTimePickerDialog](../arkts-apis-uicontext-uicontext.md#showtimepickerdialog) API in [UIContext](../arkts-apis-uicontext-uicontext.md). 97 98### Example 1: Setting the Display Time Format 99 100This example demonstrates how to set the display time using **useMilitaryTime**, **dateTimeOptions**, and **format**. 101 102```ts 103@Entry 104@Component 105struct TimePickerDialogExample { 106 private selectTime: Date = new Date('2020-12-25T08:30:00'); 107 108 build() { 109 Column() { 110 Button("TimePickerDialog 12-hour format") 111 .margin(20) 112 .onClick(() => { 113 this.getUIContext().showTimePickerDialog({ 114 selected: this.selectTime, 115 format: TimePickerFormat.HOUR_MINUTE, 116 useMilitaryTime: false, 117 dateTimeOptions: { hour: "numeric", minute: "2-digit" }, 118 onAccept: (value: TimePickerResult) => { 119 // Set selectTime to the time when the OK button is clicked. In this way, when the dialog box is displayed again, the selected time is the time when the operation was confirmed last time. 120 if (value.hour != undefined && value.minute != undefined) { 121 this.selectTime.setHours(value.hour, value.minute); 122 console.info("TimePickerDialog:onAccept()" + JSON.stringify(value)); 123 } 124 }, 125 onCancel: () => { 126 console.info("TimePickerDialog:onCancel()"); 127 }, 128 onChange: (value: TimePickerResult) => { 129 console.info("TimePickerDialog:onChange()" + JSON.stringify(value)); 130 }, 131 onDidAppear: () => { 132 console.info("TimePickerDialog:onDidAppear()"); 133 }, 134 onDidDisappear: () => { 135 console.info("TimePickerDialog:onDidDisappear()"); 136 }, 137 onWillAppear: () => { 138 console.info("TimePickerDialog:onWillAppear()"); 139 }, 140 onWillDisappear: () => { 141 console.info("TimePickerDialog:onWillDisappear()"); 142 } 143 }); 144 }) 145 Button("TimePickerDialog 24-hour format") 146 .margin(20) 147 .onClick(() => { 148 this.getUIContext().showTimePickerDialog({ 149 selected: this.selectTime, 150 format: TimePickerFormat.HOUR_MINUTE_SECOND, 151 useMilitaryTime: true, 152 onAccept: (value: TimePickerResult) => { 153 if (value.hour != undefined && value.minute != undefined) { 154 this.selectTime.setHours(value.hour, value.minute); 155 console.info("TimePickerDialog:onAccept()" + JSON.stringify(value)); 156 } 157 }, 158 }) 159 }) 160 }.width('100%') 161 } 162} 163``` 164 165 166 167 168### Example 2: Customizing the Style 169 170In this example, **disappearTextStyle**, **textStyle**, **selectedTextStyle**, **acceptButtonStyle**, and **cancelButtonStyle** are configured to customize the text and button style. 171 172```ts 173// xxx.ets 174@Entry 175@Component 176struct TimePickerDialogExample { 177 private selectTime: Date = new Date('2020-12-25T08:30:00'); 178 179 build() { 180 Column() { 181 Button("TimePickerDialog 24-hour format") 182 .margin(20) 183 .onClick(() => { 184 this.getUIContext().showTimePickerDialog({ 185 disappearTextStyle: { color: '#297bec', font: { size: 15, weight: FontWeight.Lighter } }, 186 textStyle: { color: Color.Black, font: { size: 20, weight: FontWeight.Normal } }, 187 selectedTextStyle: { color: Color.Blue, font: { size: 30, weight: FontWeight.Bolder } }, 188 acceptButtonStyle: { 189 type: ButtonType.Normal, 190 style: ButtonStyleMode.NORMAL, 191 role: ButtonRole.NORMAL, 192 fontColor: 'rgb(81, 81, 216)', 193 fontSize: '26fp', 194 fontWeight: FontWeight.Bolder, 195 fontStyle: FontStyle.Normal, 196 fontFamily: 'sans-serif', 197 backgroundColor: '#A6ACAF', 198 borderRadius: 20 199 }, 200 cancelButtonStyle: { 201 type: ButtonType.Normal, 202 style: ButtonStyleMode.NORMAL, 203 role: ButtonRole.NORMAL, 204 fontColor: Color.Blue, 205 fontSize: '16fp', 206 fontWeight: FontWeight.Normal, 207 fontStyle: FontStyle.Italic, 208 fontFamily: 'sans-serif', 209 backgroundColor: '#50182431', 210 borderRadius: 10 211 }, 212 onAccept: (value: TimePickerResult) => { 213 if (value.hour != undefined && value.minute != undefined) { 214 this.selectTime.setHours(value.hour, value.minute); 215 console.info("TimePickerDialog:onAccept()" + JSON.stringify(value)); 216 } 217 } 218 }); 219 }) 220 }.width('100%') 221 } 222} 223``` 224 225 226 227### Example 3: Configuring a Dialog Box in the Hover State 228 229This example demonstrates how to set the layout area of a dialog box in hover mode on a foldable device. 230 231```ts 232@Entry 233@Component 234struct TimePickerDialogExample { 235 private selectTime: Date = new Date('2020-12-25T08:30:00'); 236 237 build() { 238 Column() { 239 Button("TimePickerDialog 12-hour format") 240 .margin(20) 241 .onClick(() => { 242 this.getUIContext().showTimePickerDialog({ 243 selected: this.selectTime, 244 disappearTextStyle: { color: Color.Red, font: { size: 15, weight: FontWeight.Lighter } }, 245 textStyle: { color: Color.Black, font: { size: 20, weight: FontWeight.Normal } }, 246 selectedTextStyle: { color: Color.Blue, font: { size: 30, weight: FontWeight.Bolder } }, 247 onAccept: (value: TimePickerResult) => { 248 // Set selectTime to the time when the OK button is clicked. In this way, when the dialog box is displayed again, the selected time is the time when the operation was confirmed last time. 249 if (value.hour != undefined && value.minute != undefined) { 250 this.selectTime.setHours(value.hour, value.minute); 251 console.info("TimePickerDialog:onAccept()" + JSON.stringify(value)); 252 } 253 }, 254 onCancel: () => { 255 console.info("TimePickerDialog:onCancel()"); 256 }, 257 onChange: (value: TimePickerResult) => { 258 console.info("TimePickerDialog:onChange()" + JSON.stringify(value)); 259 }, 260 onDidAppear: () => { 261 console.info("TimePickerDialog:onDidAppear()"); 262 }, 263 onDidDisappear: () => { 264 console.info("TimePickerDialog:onDidDisappear()"); 265 }, 266 onWillAppear: () => { 267 console.info("TimePickerDialog:onWillAppear()"); 268 }, 269 onWillDisappear: () => { 270 console.info("TimePickerDialog:onWillDisappear()"); 271 }, 272 enableHoverMode: true, 273 hoverModeArea: HoverModeAreaType.TOP_SCREEN 274 }); 275 }) 276 }.width('100%') 277 } 278} 279``` 280 281 282 283### Example 4: Setting the Dialog Box Position 284 285This example demonstrates how to set the position of a dialog box using **alignment** and **offset**. 286 287```ts 288// xxx.ets 289@Entry 290@Component 291struct TimePickerDialogExample { 292 private selectTime: Date = new Date('2020-12-25T08:30:00'); 293 294 build() { 295 Column() { 296 Button("TimePickerDialog") 297 .margin(20) 298 .onClick(() => { 299 this.getUIContext().showTimePickerDialog({ 300 alignment: DialogAlignment.Center, 301 offset: { dx: 20 , dy: 0 }, 302 onAccept: (value: TimePickerResult) => { 303 // Set selectTime to the time when the OK button is clicked. In this way, when the dialog box is displayed again, the selected time is the time when the operation was confirmed last time. 304 if (value.hour != undefined && value.minute != undefined) { 305 this.selectTime.setHours(value.hour, value.minute); 306 console.info("TimePickerDialog:onAccept()" + JSON.stringify(value)); 307 } 308 } 309 }); 310 }) 311 }.width('100%') 312 } 313} 314``` 315 316 317 318### Example 5: Setting the Mask Area 319 320This example demonstrates how to set the mask area using **maskRect**. 321 322```ts 323// xxx.ets 324@Entry 325@Component 326struct TimePickerDialogExample { 327 private selectTime: Date = new Date('2020-12-25T08:30:00'); 328 329 build() { 330 Column() { 331 Button("TimePickerDialog") 332 .margin(20) 333 .onClick(() => { 334 this.getUIContext().showTimePickerDialog({ 335 maskRect: { x: 30, y: 60, width: '100%', height: '60%' }, 336 onAccept: (value: TimePickerResult) => { 337 // Set selectTime to the time when the OK button is clicked. In this way, when the dialog box is displayed again, the selected time is the time when the operation was confirmed last time. 338 if (value.hour != undefined && value.minute != undefined) { 339 this.selectTime.setHours(value.hour, value.minute); 340 console.info("TimePickerDialog:onAccept()" + JSON.stringify(value)); 341 } 342 } 343 }); 344 }) 345 }.width('100%') 346 } 347} 348``` 349 350 351 352### Example 6: Setting the Background 353 354This example demonstrates how to set the dialog box background using **maskRect**. 355 356```ts 357// xxx.ets 358@Entry 359@Component 360struct TimePickerDialogExample { 361 private selectTime: Date = new Date('2020-12-25T08:30:00'); 362 363 build() { 364 Column() { 365 Button("TimePickerDialog") 366 .margin(20) 367 .onClick(() => { 368 this.getUIContext().showTimePickerDialog({ 369 backgroundColor: 'rgb(204, 226, 251)', 370 backgroundBlurStyle: BlurStyle.NONE, 371 shadow: ShadowStyle.OUTER_FLOATING_SM, 372 onAccept: (value: TimePickerResult) => { 373 // Set selectTime to the time when the OK button is clicked. In this way, when the dialog box is displayed again, the selected time is the time when the operation was confirmed last time. 374 if (value.hour != undefined && value.minute != undefined) { 375 this.selectTime.setHours(value.hour, value.minute); 376 console.info("TimePickerDialog:onAccept()" + JSON.stringify(value)); 377 } 378 } 379 }); 380 }) 381 }.width('100%') 382 } 383} 384``` 385 386 387### Example 7: Setting the Start Time 388 389This example demonstrates how to set the start time for the time picker dialog box. 390 391```ts 392// xxx.ets 393@Entry 394@Component 395struct TimePickerDialogExample { 396 private selectTime: Date = new Date('2022-07-22T08:50:00'); 397 398 build() { 399 Column() { 400 Button("TimePickerDialog") 401 .margin(20) 402 .onClick(() => { 403 this.getUIContext().showTimePickerDialog({ 404 useMilitaryTime: false, 405 selected: this.selectTime, 406 format: TimePickerFormat.HOUR_MINUTE_SECOND, 407 start: new Date('2022-07-22T08:30:00'), 408 onAccept: (value: TimePickerResult) => { 409 // Set selectTime to the time when the OK button is clicked. In this way, when the dialog box is displayed again, the selected time is the time when the operation was confirmed last time. 410 if (value.hour != undefined && value.minute != undefined) { 411 this.selectTime.setHours(value.hour, value.minute); 412 console.info("TimePickerDialog:onAccept()" + JSON.stringify(value)); 413 } 414 } 415 }); 416 }) 417 }.width('100%') 418 } 419} 420``` 421 422 423### Example 8: Setting the End Time 424 425This example demonstrates how to set the end time for the time picker dialog box. 426 427```ts 428// xxx.ets 429@Entry 430@Component 431struct TimePickerDialogExample { 432 private selectTime: Date = new Date('2022-07-22T08:50:00'); 433 434 build() { 435 Column() { 436 Button("TimePickerDialog") 437 .margin(20) 438 .onClick(() => { 439 this.getUIContext().showTimePickerDialog({ 440 useMilitaryTime: false, 441 selected: this.selectTime, 442 format: TimePickerFormat.HOUR_MINUTE_SECOND, 443 end: new Date('2022-07-22T15:20:00'), 444 onAccept: (value: TimePickerResult) => { 445 // Set selectTime to the time when the OK button is clicked. In this way, when the dialog box is displayed again, the selected time is the time when the operation was confirmed last time. 446 if (value.hour != undefined && value.minute != undefined) { 447 this.selectTime.setHours(value.hour, value.minute); 448 console.info("TimePickerDialog:onAccept()" + JSON.stringify(value)); 449 } 450 } 451 }); 452 }) 453 }.width('100%') 454 } 455} 456``` 457 458 459### Example 9: Enabling the AM/PM Indicator to Automatically Switch Based on the Hour in 12-hour Format 460 461This example demonstrates how to enable AM/PM indicator to automatically switch based on the hour in 12-hour format using **enableCascade**. 462 463```ts 464// xxx.ets 465@Entry 466@Component 467struct TimePickerDialogExample { 468 private selectTime: Date = new Date('2022-07-22T08:00:00'); 469 470 build() { 471 Column() { 472 Button("TimePickerDialog") 473 .margin(20) 474 .onClick(() => { 475 this.getUIContext().showTimePickerDialog({ 476 useMilitaryTime: false, 477 selected: this.selectTime, 478 enableCascade:true, 479 onAccept: (value: TimePickerResult) => { 480 // Set selectTime to the time when the OK button is clicked. In this way, when the dialog box is displayed again, the selected time is the time when the operation was confirmed last time. 481 if (value.hour != undefined && value.minute != undefined) { 482 this.selectTime.setHours(value.hour, value.minute); 483 console.info("TimePickerDialog:onAccept()" + JSON.stringify(value)); 484 } 485 } 486 }); 487 }) 488 }.width('100%') 489 } 490} 491``` 492 493 494 495### Example 10: Customizing the Background Blur Effect 496 497This example demonstrates how to customize the background blur effect by configuring [backgroundBlurStyleOptions](#timepickerdialogoptions). This functionality is supported since API version 19. 498 499```ts 500@Entry 501@Component 502struct TimePickerDialogExample { 503 build() { 504 Stack({ alignContent: Alignment.Top }) { 505 Image($r('app.media.bg')) 506 Column() { 507 Button("TimePickerDialog") 508 .margin(20) 509 .onClick(() => { 510 this.getUIContext().showTimePickerDialog({ 511 backgroundColor: undefined, 512 backgroundBlurStyle: BlurStyle.Thin, 513 backgroundBlurStyleOptions: { 514 colorMode: ThemeColorMode.LIGHT, 515 adaptiveColor: AdaptiveColor.AVERAGE, 516 scale: 1, 517 blurOptions: { grayscale: [20, 20] }, 518 }, 519 }); 520 }) 521 }.width('100%') 522 } 523 } 524} 525``` 526 527 528 529### Example 11: Customizing the Background Effect 530 531This example demonstrates how to customize the background effect by configuring [backgroundEffect](#timepickerdialogoptions). This functionality is supported since API version 19. 532 533```ts 534@Entry 535@Component 536struct TimePickerDialogExample { 537 build() { 538 Stack({ alignContent: Alignment.Top }) { 539 Image($r('app.media.bg')) 540 Column() { 541 Button("TimePickerDialog") 542 .margin(20) 543 .onClick(() => { 544 this.getUIContext().showTimePickerDialog({ 545 backgroundColor: undefined, 546 backgroundBlurStyle: BlurStyle.Thin, 547 backgroundEffect: { 548 radius: 60, 549 saturation: 0, 550 brightness: 1, 551 color: Color.White, 552 blurOptions: { grayscale: [20, 20] } 553 }, 554 }); 555 }) 556 }.width('100%') 557 } 558 } 559} 560``` 561 562 563