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


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

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

E
ester.zhou 已提交
10 11 12 13 14 15 16 17

## Required Permissions

No


## Child Components

E
ester.zhou 已提交
18
Not supported
E
ester.zhou 已提交
19 20 21 22 23 24 25 26 27


## APIs

TimePicker(options?: TimePickerOptions)

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

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


## Attributes

H
HelloCrease 已提交
35 36 37
| 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 已提交
38 39 40 41


## Events

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

### TimePickerResult
H
HelloCrease 已提交
47 48 49 50
| Name   | Type   | Description                          |
| ------ | ------ | ------------------------------------ |
| hour   | number | Hour portion of the selected time.   |
| minute | number | Minute portion of the selected time. |
E
ester.zhou 已提交
51 52 53 54 55 56 57


## Example


### Time Picker

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

  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)