ts-basic-components-timepicker.md 1.7 KB
Newer Older
E
ester.zhou 已提交
1 2 3 4
# TimePicker

The **\<TimePicker>** component allows users to select a time from the given range.

E
ester.zhou 已提交
5
>  **NOTE**
E
ester.zhou 已提交
6
>
E
ester.zhou 已提交
7
>  This component is supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.
E
ester.zhou 已提交
8 9 10 11


## Child Components

E
ester.zhou 已提交
12
Not supported
E
ester.zhou 已提交
13 14 15 16


## APIs

E
ester.zhou 已提交
17
TimePicker(options?: {selected?: Date})
E
ester.zhou 已提交
18 19 20

Creates a time picker whose default time range is from 00:00 to 23:59.

E
ester.zhou 已提交
21 22 23 24 25
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| selected | Date | No| Time of the selected item.<br>Default value: current system time|
E
ester.zhou 已提交
26 27 28 29


## Attributes

E
ester.zhou 已提交
30 31 32
| Name| Type| Description|
| -------- | -------- | -------- |
| useMilitaryTime | boolean | Whether to display time in 24-hour format. The value cannot be modified dynamically.<br>Default value: **false**|
E
ester.zhou 已提交
33 34 35 36


## Events

E
ester.zhou 已提交
37 38 39
| Name                                      | Description       |
| ---------------------------------------- | ----------- |
| onChange(callback: (value: TimePickerResult ) =&gt; void) | Triggered when a time is selected.|
E
ester.zhou 已提交
40 41

### TimePickerResult
E
ester.zhou 已提交
42 43 44 45
| Name    | Type  | Description     |
| ------ | ------ | ------- |
| hour   | number | Hour portion of the selected time.|
| minute | number | Minute portion of the selected time.|
E
ester.zhou 已提交
46 47 48 49 50


## Example


E
ester.zhou 已提交
51
### Time Selector
E
ester.zhou 已提交
52

E
ester.zhou 已提交
53 54
```ts
// xxx.ets
E
ester.zhou 已提交
55 56 57
@Entry
@Component
struct TimePickerExample {
E
ester.zhou 已提交
58
  private selectedTime: Date = new Date('7/22/2022 8:00:00')
E
ester.zhou 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

  build() {
    Column() {
      TimePicker({
        selected: this.selectedTime,
      })
      .useMilitaryTime(true)
      .onChange((date: TimePickerResult) => {
        console.info('select current date is: ' + JSON.stringify(date))
      })
    }.width('100%')
  }
}
```

![en-us_image_0000001251292933](figures/en-us_image_0000001251292933.gif)