• 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?: {selected?: Date})
18
19Creates a time picker, which is in 24-hour format by default.
20
21**Parameters**
22
23| Name  | Type| Mandatory| Description                                                    |
24| -------- | -------- | ---- | ------------------------------------------------------------ |
25| 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.|
26
27## Attributes
28
29In addition to the [universal attributes](ts-universal-attributes-size.md), the following attributes are supported.
30
31| Name                            | Type                                                    | Description                                                        |
32| -------------------------------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
33| 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.|
34| 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>} |
35| 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>} |
36| 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>} |
37
38## Events
39
40In addition to the [universal events](ts-universal-events-click.md), the following events are supported.
41
42| Name                                      | Description       |
43| ---------------------------------------- | ----------- |
44| onChange(callback: (value: TimePickerResult ) =&gt; void) | Triggered when a time is selected.|
45
46## TimePickerResult
47
48Describes a time in 24-hour format.
49
50| Name  | Type| Description                               |
51| ------ | -------- | ----------------------------------- |
52| hour   | number   | Hour portion of the selected time.<br>Value range: [0-23]|
53| minute | number   | Minute portion of the selected time.<br>Value range: [0-59]|
54
55
56## Example
57
58```ts
59// xxx.ets
60@Entry
61@Component
62struct TimePickerExample {
63  @State isMilitaryTime: boolean = false
64  private selectedTime: Date = new Date('2022-07-22T08:00:00')
65
66  build() {
67    Column() {
68      Button ('Switch Time Format')
69        .margin({ top: 30 })
70        .onClick(() => {
71          this.isMilitaryTime = !this.isMilitaryTime
72        })
73      TimePicker({
74        selected: this.selectedTime,
75      })
76        .useMilitaryTime(this.isMilitaryTime)
77        .onChange((value: TimePickerResult) => {
78          this.selectedTime.setHours(value.hour, value.minute)
79          console.info('select current date is: ' + JSON.stringify(value))
80        })
81        .disappearTextStyle({color: Color.Red, font: {size: 15, weight: FontWeight.Lighter}})
82        .textStyle({color: Color.Black, font: {size: 20, weight: FontWeight.Normal}})
83        .selectedTextStyle({color: Color.Blue, font: {size: 30, weight: FontWeight.Bolder}})
84    }.width('100%')
85  }
86}
87```
88
89![timePicker](figures/timePicker.gif)
90