• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# TimePicker
2
3The **\<TimePicker>** component allows users to select a time (with the hour and minute) from the given range.
4
5>  **NOTE**
6>
7>  This component is supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.
8
9
10## Child Components
11
12Not supported
13
14
15## APIs
16
17TimePicker(options?: TimePickerOptions)
18
19Creates a time picker, which is in 24-hour format by default.
20
21**System capability**: SystemCapability.ArkUI.ArkUI.Full
22
23**Parameters**
24
25| Name | Type                                           | Mandatory| Description                    |
26| ------- | ----------------------------------------------- | ---- | ------------------------ |
27| options | [TimePickerOptions](#timepickeroptions) | No  | Parameters of the time picker.|
28
29## TimePickerOptions
30
31| Name                | Type                                         | Mandatory| Description                                                        |
32| -------------------- | --------------------------------------------- | ---- | ------------------------------------------------------------ |
33| selected             | Date                                          | No  | Time of the selected item.<br>Default value: current system time<br>Since API version 10, this parameter supports [$$](../../quick-start/arkts-two-way-sync.md) for two-way binding of variables.|
34
35## Attributes
36
37In addition to the [universal attributes](ts-universal-attributes-size.md), the following attributes are supported.
38
39| Name                            | Type                                                    | Description                                                        |
40| -------------------------------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
41| useMilitaryTime                  | boolean                                                      | Whether the display time is in 24-hour format.<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.|
42| disappearTextStyle<sup>10+</sup> | [PickerTextStyle](ts-basic-components-datepicker.md#pickertextstyle10) | 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>} |
43| textStyle<sup>10+</sup>          | [PickerTextStyle](ts-basic-components-datepicker.md#pickertextstyle10) | 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>} |
44| selectedTextStyle<sup>10+</sup>  | [PickerTextStyle](ts-basic-components-datepicker.md#pickertextstyle10) | 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>} |
45
46## Events
47
48In addition to the [universal events](ts-universal-events-click.md), the following events are supported.
49
50| Name                                      | Description       |
51| ---------------------------------------- | ----------- |
52| onChange(callback: (value: TimePickerResult ) =&gt; void) | Triggered when a time is selected.|
53
54## TimePickerResult
55
56Describes a time in 24-hour format.
57
58| Name  | Type| Description                               |
59| ------ | -------- | ----------------------------------- |
60| hour   | number   | Hour portion of the selected time.<br>Value range: [0-23]|
61| minute | number   | Minute portion of the selected time.<br>Value range: [0-59]|
62
63
64## Example
65
66```ts
67// xxx.ets
68@Entry
69@Component
70struct TimePickerExample {
71  @State isMilitaryTime: boolean = false
72  private selectedTime: Date = new Date('2022-07-22T08:00:00')
73
74  build() {
75    Column() {
76      Button ('Switch Time Format')
77        .margin(30)
78        .onClick(() => {
79          this.isMilitaryTime = !this.isMilitaryTime
80        })
81      TimePicker({
82        selected: this.selectedTime,
83      })
84        .useMilitaryTime(this.isMilitaryTime)
85        .onChange((value: TimePickerResult) => {
86          if(value.hour) {
87            this.selectedTime.setHours(value.hour, value.minute)
88            console.info('select current date is: ' + JSON.stringify(value))
89          }
90        })
91        .disappearTextStyle({color: Color.Red, font: {size: 15, weight: FontWeight.Lighter}})
92        .textStyle({color: Color.Black, font: {size: 20, weight: FontWeight.Normal}})
93        .selectedTextStyle({color: Color.Blue, font: {size: 30, weight: FontWeight.Bolder}})
94    }.width('100%')
95  }
96}
97```
98
99![timePicker](figures/timePicker.gif)
100