• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Time Picker Dialog Box (TimePickerDialog)
2
3A time picker dialog box is a dialog box that allows users to select a time from the 24-hour range through scrolling.
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](../js-apis-arkui-UIContext.md#uicontext).
10>
11> Since API version 10, you can use the [showTimePickerDialog](../js-apis-arkui-UIContext.md#showtimepickerdialog) API in [UIContext](../js-apis-arkui-UIContext.md#uicontext), which ensures that the time picker dialog box is shown in the intended UI instance.
12
13## TimePickerDialog
14
15### show
16
17static show(options?: TimePickerDialogOptions)
18
19Shows a time picker dialog box.
20
21**Atomic service API**: This API can be used in atomic services since API version 11.
22
23**System capability**: SystemCapability.ArkUI.ArkUI.Full
24
25**Parameters**
26
27| Name | Type                                                       | Mandatory| Description                      |
28| ------- | ----------------------------------------------------------- | ---- | -------------------------- |
29| options | [TimePickerDialogOptions](#timepickerdialogoptions) | No  | Parameters of the time picker dialog box.|
30
31## TimePickerDialogOptions
32
33Inherited from [TimePickerOptions](ts-basic-components-timepicker.md#timepickeroptions).
34
35**System capability**: SystemCapability.ArkUI.ArkUI.Full
36
37| Name| Type| Mandatory| Description|
38| -------- | -------- | -------- | -------- |
39| useMilitaryTime | boolean | No| 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.|
40| disappearTextStyle<sup>10+</sup> | [PickerTextStyle](ts-basic-components-datepicker.md#pickertextstyle10) | No| Font color, font size, and font weight for the top and bottom items.<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.|
41| textStyle<sup>10+</sup> | [PickerTextStyle](ts-basic-components-datepicker.md#pickertextstyle10) | No| Font color, font size, and font weight 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>}<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
42| selectedTextStyle<sup>10+</sup> | [PickerTextStyle](ts-basic-components-datepicker.md#pickertextstyle10) | No| Font color, font size, and font weight of the selected item.<br>Default value:<br>{<br>color: '#ff007dff',<br>font: {<br>size: '20vp', <br>weight: FontWeight.Medium<br>}<br>}<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
43| acceptButtonStyle<sup>12+</sup> | [PickerDialogButtonStyle](ts-methods-datepicker-dialog.md#pickerdialogbuttonstyle12) | No| Style of the accept button.<br>**NOTE**<br>In the **acceptButtonStyle** and **cancelButtonStyle** configurations, only one **primary** field can be set to **true** at most. If both the **primary** fields are set to **true**, neither will take effect.<br>**Atomic service API**: This API can be used in atomic services since API version 12.|
44| cancelButtonStyle<sup>12+</sup> | [PickerDialogButtonStyle](ts-methods-datepicker-dialog.md#pickerdialogbuttonstyle12) | No| Style of the cancel button.<br>**NOTE**<br>In the **acceptButtonStyle** and **cancelButtonStyle** configurations, only one **primary** field can be set to **true** at most. If both the **primary** fields are set to **true**, neither will take effect.<br>**Atomic service API**: This API can be used in atomic services since API version 12.|
45| 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**<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
46| 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 }**<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
47| 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%' }**<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
48| onAccept | (value: [TimePickerResult](ts-basic-components-timepicker.md#TimePickerResult)) => void | No| 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.|
49| onCancel | () => void | No| 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.|
50| onChange | (value: [TimePickerResult](ts-basic-components-timepicker.md#TimePickerResult)) => void | No| Callback invoked 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.|
51| backgroundColor<sup>11+</sup> | [ResourceColor](ts-types.md#resourcecolor)  | No| 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.|
52| backgroundBlurStyle<sup>11+</sup> | [BlurStyle](ts-universal-attributes-background.md#blurstyle9) | No| 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.|
53| onDidAppear<sup>12+</sup> | () => void | No| 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.|
54| onDidDisappear<sup>12+</sup> | () => void | No| 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.|
55| onWillAppear<sup>12+</sup> | () => void | No| 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.|
56| onWillDisappear<sup>12+</sup> | () => void | No| 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.|
57| shadow<sup>12+</sup>              | [ShadowOptions](ts-universal-attributes-image-effect.md#shadowoptions) \| [ShadowStyle](ts-universal-attributes-image-effect.md#shadowstyle10) | No  | 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.|
58| dateTimeOptions<sup>12+</sup> | [DateTimeOptions](../../apis-localization-kit/js-apis-intl.md#datetimeoptions) | No| 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**: In the 24-hour format, it defaults to **2-digit**, which means a leading zero is used; In the 12-hour format, it defaults to **numeric**, which means no leading zero is used.<br>**minute**: defaults to **2-digit**, which means a leading zero is used.<br>**Atomic service API**: This API can be used in atomic services since API version 12.|
59| enableHoverMode<sup>14+</sup>     | boolean | No  | 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.|
60| hoverModeArea<sup>14+</sup>       | [HoverModeAreaType](ts-appendix-enums.md#hovermodeareatype14) | No  | 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.|
61| onEnterSelectedArea<sup>16+</sup>   |  Callback\<[TimePickerResult](ts-basic-components-timepicker.md#TimePickerResult)> | No  |  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 16.|
62| enableCascade<sup>16+</sup>              | boolean | No  | 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 16.|
63| enableHapticFeedback<sup>16+</sup> | boolean | No  | 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 16.<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"}]|
64
65**Handling in the case of exceptions**
66
67| Exception  | Result |
68| -------- |  ------------------------------------------------------------ |
69| The start time is later than the end time.   | Both start time and end time are set to their default values. |
70| The selected time is earlier than the start time.   | The selected time is set to the start time. |
71| The selected time is later than the end time.   | The selected time is set to the end time. |
72| 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.|
73| 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. |
74| The time format is invalid, such as **'01:61:61'**.  | The default value is used. |
75
76## Example
77
78>  **NOTE**
79>
80> For clarity in UI execution context, you are advised to use the [showTimePickerDialog](../js-apis-arkui-UIContext.md#showtimepickerdialog) API in [UIContext](../js-apis-arkui-UIContext.md#uicontext).
81
82### Example 1: Setting the Display Time
83
84This example demonstrates how to set the display time using **useMilitaryTime**, **dateTimeOptions**, and **format**.
85
86```ts
87@Entry
88@Component
89struct TimePickerDialogExample {
90  private selectTime: Date = new Date('2020-12-25T08:30:00')
91
92  build() {
93    Column() {
94      Button("TimePickerDialog 12-hour format")
95        .margin(20)
96        .onClick(() => {
97          TimePickerDialog.show({
98            selected: this.selectTime,
99            format: TimePickerFormat.HOUR_MINUTE,
100            useMilitaryTime: false,
101            dateTimeOptions: { hour: "numeric", minute: "2-digit" },
102            onAccept: (value: TimePickerResult) => {
103              // 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.
104              if (value.hour != undefined && value.minute != undefined) {
105                this.selectTime.setHours(value.hour, value.minute)
106                console.info("TimePickerDialog:onAccept()" + JSON.stringify(value))
107              }
108            },
109            onCancel: () => {
110              console.info("TimePickerDialog:onCancel()")
111            },
112            onChange: (value: TimePickerResult) => {
113              console.info("TimePickerDialog:onChange()" + JSON.stringify(value))
114            },
115            onDidAppear: () => {
116              console.info("TimePickerDialog:onDidAppear()")
117            },
118            onDidDisappear: () => {
119              console.info("TimePickerDialog:onDidDisappear()")
120            },
121            onWillAppear: () => {
122              console.info("TimePickerDialog:onWillAppear()")
123            },
124            onWillDisappear: () => {
125              console.info("TimePickerDialog:onWillDisappear()")
126            }
127          })
128        })
129      Button("TimePickerDialog 24-hour format")
130        .margin(20)
131        .onClick(() => {
132          TimePickerDialog.show({
133            selected: this.selectTime,
134            format: TimePickerFormat.HOUR_MINUTE_SECOND,
135            useMilitaryTime: true,
136            onAccept: (value: TimePickerResult) => {
137              if (value.hour != undefined && value.minute != undefined) {
138                this.selectTime.setHours(value.hour, value.minute)
139                console.info("TimePickerDialog:onAccept()" + JSON.stringify(value))
140              }
141            },
142          })
143        })
144    }.width('100%')
145  }
146}
147```
148
149![TimetPickerDialog](figures/TimePickerDialog.gif)
150
151
152### Example 2: Customizing the Style
153
154In this example, **disappearTextStyle**, **textStyle**, **selectedTextStyle**, **acceptButtonStyle**, and **cancelButtonStyle** are configured to customize the text and button style.
155
156```ts
157// xxx.ets
158@Entry
159@Component
160struct TimePickerDialogExample {
161  private selectTime: Date = new Date('2020-12-25T08:30:00')
162
163  build() {
164    Column() {
165      Button("TimePickerDialog 24-hour format")
166        .margin(20)
167        .onClick(() => {
168          TimePickerDialog.show({
169            disappearTextStyle: { color: '#297bec', font: { size: 15, weight: FontWeight.Lighter } },
170            textStyle: { color: Color.Black, font: { size: 20, weight: FontWeight.Normal } },
171            selectedTextStyle: { color: Color.Blue, font: { size: 30, weight: FontWeight.Bolder } },
172            acceptButtonStyle: {
173              type: ButtonType.Normal,
174              style: ButtonStyleMode.NORMAL,
175              role: ButtonRole.NORMAL,
176              fontColor: 'rgb(81, 81, 216)',
177              fontSize: '26fp',
178              fontWeight: FontWeight.Bolder,
179              fontStyle: FontStyle.Normal,
180              fontFamily: 'sans-serif',
181              backgroundColor: '#A6ACAF',
182              borderRadius: 20
183            },
184            cancelButtonStyle: {
185              type: ButtonType.Normal,
186              style: ButtonStyleMode.NORMAL,
187              role: ButtonRole.NORMAL,
188              fontColor: Color.Blue,
189              fontSize: '16fp',
190              fontWeight: FontWeight.Normal,
191              fontStyle: FontStyle.Italic,
192              fontFamily: 'sans-serif',
193              backgroundColor: '#50182431',
194              borderRadius: 10
195            },
196            onAccept: (value: TimePickerResult) => {
197              if (value.hour != undefined && value.minute != undefined) {
198                this.selectTime.setHours(value.hour, value.minute)
199                console.info("TimePickerDialog:onAccept()" + JSON.stringify(value))
200              }
201            }
202          })
203        })
204    }.width('100%')
205  }
206}
207```
208
209![TimetPickerDialog](figures/TimePickerDialog_CustomButton.png)
210
211### Example 3: Configuring a Dialog Box in the Hover State
212
213This example demonstrates how to set the layout area of a dialog box in hover mode on a foldable device.
214
215```ts
216@Entry
217@Component
218struct TimePickerDialogExample {
219  private selectTime: Date = new Date('2020-12-25T08:30:00');
220
221  build() {
222    Column() {
223      Button("TimePickerDialog 12-hour format")
224        .margin(20)
225        .onClick(() => {
226          TimePickerDialog.show ({ // You are advised to use this.getUIContext().showTimePickerDialog().
227            selected: this.selectTime,
228            disappearTextStyle: { color: Color.Red, font: { size: 15, weight: FontWeight.Lighter } },
229            textStyle: { color: Color.Black, font: { size: 20, weight: FontWeight.Normal } },
230            selectedTextStyle: { color: Color.Blue, font: { size: 30, weight: FontWeight.Bolder } },
231            onAccept: (value: TimePickerResult) => {
232              // 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.
233              if (value.hour != undefined && value.minute != undefined) {
234                this.selectTime.setHours(value.hour, value.minute);
235                console.info("TimePickerDialog:onAccept()" + JSON.stringify(value));
236              }
237            },
238            onCancel: () => {
239              console.info("TimePickerDialog:onCancel()");
240            },
241            onChange: (value: TimePickerResult) => {
242              console.info("TimePickerDialog:onChange()" + JSON.stringify(value));
243            },
244            onDidAppear: () => {
245              console.info("TimePickerDialog:onDidAppear()");
246            },
247            onDidDisappear: () => {
248              console.info("TimePickerDialog:onDidDisappear()");
249            },
250            onWillAppear: () => {
251              console.info("TimePickerDialog:onWillAppear()");
252            },
253            onWillDisappear: () => {
254              console.info("TimePickerDialog:onWillDisappear()");
255            },
256            enableHoverMode: true,
257            hoverModeArea: HoverModeAreaType.TOP_SCREEN
258          })
259        })
260    }.width('100%')
261  }
262}
263```
264
265
266
267### Example 4: Setting the Dialog Box Position
268
269This example demonstrates how to set the position of a dialog box using **alignment** and **offset**.
270
271```ts
272// xxx.ets
273@Entry
274@Component
275struct TimePickerDialogExample {
276  private selectTime: Date = new Date('2020-12-25T08:30:00')
277
278  build() {
279    Column() {
280      Button("TimePickerDialog")
281        .margin(20)
282        .onClick(() => {
283          TimePickerDialog.show({
284            alignment: DialogAlignment.Center,
285            offset: { dx: 20 , dy: 0 },
286            onAccept: (value: TimePickerResult) => {
287              // 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.
288              if (value.hour != undefined && value.minute != undefined) {
289                this.selectTime.setHours(value.hour, value.minute)
290                console.info("TimePickerDialog:onAccept()" + JSON.stringify(value))
291              }
292            }
293          })
294        })
295    }.width('100%')
296  }
297}
298```
299
300
301
302### Example 5: Setting the Mask Area
303
304This example demonstrates how to set the mask area using **maskRect**.
305
306```ts
307// xxx.ets
308@Entry
309@Component
310struct TimePickerDialogExample {
311  private selectTime: Date = new Date('2020-12-25T08:30:00')
312
313  build() {
314    Column() {
315      Button("TimePickerDialog")
316        .margin(20)
317        .onClick(() => {
318          TimePickerDialog.show({
319            maskRect: { x: 30, y: 60, width: '100%', height: '60%' },
320            onAccept: (value: TimePickerResult) => {
321              // 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.
322              if (value.hour != undefined && value.minute != undefined) {
323                this.selectTime.setHours(value.hour, value.minute)
324                console.info("TimePickerDialog:onAccept()" + JSON.stringify(value))
325              }
326            }
327          })
328        })
329    }.width('100%')
330  }
331}
332```
333
334
335
336### Example 6: Setting the Background
337
338This example demonstrates how to set the mask area using **maskRect**.
339
340```ts
341// xxx.ets
342@Entry
343@Component
344struct TimePickerDialogExample {
345  private selectTime: Date = new Date('2020-12-25T08:30:00')
346
347  build() {
348    Column() {
349      Button("TimePickerDialog")
350        .margin(20)
351        .onClick(() => {
352          TimePickerDialog.show({
353            backgroundColor: 'rgb(204, 226, 251)',
354            backgroundBlurStyle: BlurStyle.NONE,
355            shadow: ShadowStyle.OUTER_FLOATING_SM,
356            onAccept: (value: TimePickerResult) => {
357              // 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.
358              if (value.hour != undefined && value.minute != undefined) {
359                this.selectTime.setHours(value.hour, value.minute)
360                console.info("TimePickerDialog:onAccept()" + JSON.stringify(value))
361              }
362            }
363          })
364        })
365    }.width('100%')
366  }
367}
368```
369
370
371### Example 7: Setting the Start Time
372
373This example demonstrates how to set the start time for the time picker dialog box.
374
375```ts
376// xxx.ets
377@Entry
378@Component
379struct TimePickerDialogExample {
380  private selectTime: Date = new Date('2022-07-22T08:50:00')
381
382  build() {
383    Column() {
384      Button("TimePickerDialog")
385        .margin(20)
386        .onClick(() => {
387          TimePickerDialog.show({
388            useMilitaryTime: false,
389            selected: this.selectTime,
390            format: TimePickerFormat.HOUR_MINUTE_SECOND,
391            start: new Date('2022-07-22T08:30:00'),
392            onAccept: (value: TimePickerResult) => {
393              // 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.
394              if (value.hour != undefined && value.minute != undefined) {
395                this.selectTime.setHours(value.hour, value.minute)
396                console.info("TimePickerDialog:onAccept()" + JSON.stringify(value))
397              }
398            }
399          })
400        })
401    }.width('100%')
402  }
403}
404```
405
406
407### Example 8: Setting the End Time
408
409This example demonstrates how to set the end time for the time picker dialog box.
410
411```ts
412// xxx.ets
413@Entry
414@Component
415struct TimePickerDialogExample {
416  private selectTime: Date = new Date('2022-07-22T08:50:00')
417
418  build() {
419    Column() {
420      Button("TimePickerDialog")
421        .margin(20)
422        .onClick(() => {
423          TimePickerDialog.show({
424            useMilitaryTime: false,
425            selected: this.selectTime,
426            format: TimePickerFormat.HOUR_MINUTE_SECOND,
427            end: new Date('2022-07-22T15:20:00'),
428            onAccept: (value: TimePickerResult) => {
429              // 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.
430              if (value.hour != undefined && value.minute != undefined) {
431                this.selectTime.setHours(value.hour, value.minute)
432                console.info("TimePickerDialog:onAccept()" + JSON.stringify(value))
433              }
434            }
435          })
436        })
437    }.width('100%')
438  }
439}
440```
441
442
443### Example 9: Enabling the AM/PM Indicator to Automatically Switch Based on the Hour in 12-hour Format
444
445This example demonstrates how to enable AM/PM indicator to automatically switch based on the hour in 12-hour format using **enableCascade**.
446
447```ts
448// xxx.ets
449@Entry
450@Component
451struct TimePickerDialogExample {
452  private selectTime: Date = new Date('2022-07-22T08:00:00')
453
454  build() {
455    Column() {
456      Button("TimePickerDialog")
457        .margin(20)
458        .onClick(() => {
459          TimePickerDialog.show({
460            useMilitaryTime: false,
461            selected: this.selectTime,
462            enableCascade:true,
463            onAccept: (value: TimePickerResult) => {
464              // 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.
465              if (value.hour != undefined && value.minute != undefined) {
466                this.selectTime.setHours(value.hour, value.minute)
467                console.info("TimePickerDialog:onAccept()" + JSON.stringify(value))
468              }
469            }
470          })
471        })
472    }.width('100%')
473  }
474}
475```
476
477
478