• 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](../apis/js-apis-arkui-UIContext.md#uicontext).
10>
11> Since API version 10, you can use the [showTimePickerDialog](../apis/js-apis-arkui-UIContext.md#showtimepickerdialog) API in [UIContext](../apis/js-apis-arkui-UIContext.md#uicontext) to obtain the UI context.
12
13## TimePickerDialog.show
14
15show(options?: TimePickerDialogOptions)
16
17Shows a time picker dialog box.
18
19**System capability**: SystemCapability.ArkUI.ArkUI.Full
20
21**Parameters**
22
23| Name | Type                                                       | Mandatory| Description                      |
24| ------- | ----------------------------------------------------------- | ---- | -------------------------- |
25| options | [TimePickerDialogOptions](#timepickerdialogoptions) | No  | Parameters of the time picker dialog box.|
26
27## TimePickerDialogOptions
28
29Inherited from [TimePickerOptions](ts-basic-components-timepicker.md#timepickeroptions).
30
31
32| Name| Type| Mandatory| Description|
33| -------- | -------- | -------- | -------- |
34| 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.|
35| disappearTextStyle<sup>10+</sup> | [PickerTextStyle](ts-basic-components-datepicker.md#pickertextstyle10) | No| Font color, font size, and font width for the top and bottom items.<br>Default value:<br>{<br>color: '#ff182431',<br>font: {<br>size: '14fp', <br>weight: FontWeight.Regular<br>}<br>} |
36| textStyle<sup>10+</sup> | [PickerTextStyle](ts-basic-components-datepicker.md#pickertextstyle10) | No| Font color, font size, and font width of all items except the top, bottom, and selected items.<br>Default value:<br>{<br>color: '#ff182431',<br>font: {<br>size: '16fp', <br>weight: FontWeight.Regular<br>}<br>} |
37| selectedTextStyle<sup>10+</sup> | [PickerTextStyle](ts-basic-components-datepicker.md#pickertextstyle10) | No| Font color, font size, and font width of the selected item.<br>Default value:<br>{<br>color: '#ff007dff',<br>font: {<br>size: '20vp', <br>weight: FontWeight.Medium<br>}<br>} |
38| 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**|
39| 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 }**|
40| 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%' }**|
41| onAccept | (value: [TimePickerResult](ts-basic-components-timepicker.md#TimePickerResult)) => void | No| Callback invoked when the OK button in the dialog box is clicked.|
42| onCancel | () => void | No| Callback invoked when the Cancel button in the dialog box is clicked.|
43| onChange | (value: [TimePickerResult](ts-basic-components-timepicker.md#TimePickerResult)) => void | No| Callback invoked when the selected time changes.|
44| backgroundColor<sup>11+</sup> | [ResourceColor](ts-types.md#resourcecolor)  | No| Backplane color of the dialog box.<br>Default value: **Color.Transparent**|
45| backgroundBlurStyle<sup>11+</sup> | [BlurStyle](ts-appendix-enums.md#blurstyle9) | No| Background blur style of the dialog box.<br>Default value: **BlurStyle.COMPONENT_ULTRA_THICK**|
46
47## Example
48
49```ts
50// xxx.ets
51@Entry
52@Component
53struct TimePickerDialogExample {
54  private selectTime: Date = new Date('2020-12-25T08:30:00')
55
56  build() {
57    Column() {
58      Button ("TimePickerDialog 12-hour format")
59        .margin(20)
60        .onClick(() => {
61          TimePickerDialog.show({
62            selected: this.selectTime,
63            disappearTextStyle: { color: Color.Red, font: { size: 15, weight: FontWeight.Lighter } },
64            textStyle: { color: Color.Black, font: { size: 20, weight: FontWeight.Normal } },
65            selectedTextStyle: { color: Color.Blue, font: { size: 30, weight: FontWeight.Bolder } },
66            onAccept: (value: TimePickerResult) => {
67              // 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.
68              if (value.hour != undefined && value.minute != undefined) {
69                this.selectTime.setHours(value.hour, value.minute)
70                console.info("TimePickerDialog:onAccept()" + JSON.stringify(value))
71              }
72            },
73            onCancel: () => {
74              console.info("TimePickerDialog:onCancel()")
75            },
76            onChange: (value: TimePickerResult) => {
77              console.info("TimePickerDialog:onChange()" + JSON.stringify(value))
78            }
79          })
80        })
81      Button ("TimePickerDialog 24-hour format")
82        .margin(20)
83        .onClick(() => {
84          TimePickerDialog.show({
85            selected: this.selectTime,
86            useMilitaryTime: true,
87            disappearTextStyle: { color: Color.Red, font: { size: 15, weight: FontWeight.Lighter } },
88            textStyle: { color: Color.Black, font: { size: 20, weight: FontWeight.Normal } },
89            selectedTextStyle: { color: Color.Blue, font: { size: 30, weight: FontWeight.Bolder } },
90            onAccept: (value: TimePickerResult) => {
91              if (value.hour != undefined && value.minute != undefined) {
92                this.selectTime.setHours(value.hour, value.minute)
93                console.info("TimePickerDialog:onAccept()" + JSON.stringify(value))
94              }
95            },
96            onCancel: () => {
97              console.info("TimePickerDialog:onCancel()")
98            },
99            onChange: (value: TimePickerResult) => {
100              console.info("TimePickerDialog:onChange()" + JSON.stringify(value))
101            }
102          })
103        })
104    }.width('100%')
105  }
106}
107```
108
109![TimetPickerDialog](figures/TimePickerDialog.gif)
110