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

3
> **NOTE**<br>
E
ester.zhou 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
> This component is supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.


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


## Required Permissions

No


## Child Components

No


## APIs

TimePicker(options?: TimePickerOptions)

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

- options parameters
H
HelloCrease 已提交
27 28 29
  | Name     | Type | Mandatory | Default Value       | Description                |
  | -------- | ---- | --------- | ------------------- | -------------------------- |
  | selected | Date | No        | Current system time | Time of the selected item. |
E
ester.zhou 已提交
30 31 32 33


## Attributes

H
HelloCrease 已提交
34 35 36
| Name            | Type    | Default Value | Description                              |
| --------------- | ------- | ------------- | ---------------------------------------- |
| useMilitaryTime | boolean | false         | Whether to display time in 24-hour format. The value cannot be modified dynamically. |
E
ester.zhou 已提交
37 38 39 40


## Events

H
HelloCrease 已提交
41 42 43
| Name                                     | Description                        |
| ---------------------------------------- | ---------------------------------- |
| onChange(callback:&nbsp;(value:&nbsp;TimePickerResult )&nbsp;=&gt;&nbsp;void) | Triggered when a time is selected. |
E
ester.zhou 已提交
44 45

### TimePickerResult
H
HelloCrease 已提交
46 47 48 49
| Name   | Type   | Description                          |
| ------ | ------ | ------------------------------------ |
| hour   | number | Hour portion of the selected time.   |
| minute | number | Minute portion of the selected time. |
E
ester.zhou 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77


## Example


### Time Picker

```
@Entry
@Component
struct TimePickerExample {
  private selectedTime: Date = new Date('08-00')

  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)