ts-basic-components-datepicker.md 2.3 KB
Newer Older
Z
zengyawen 已提交
1 2
# DatePicker

E
ester.zhou 已提交
3
The **\<DatePicker>** component allows users to select a date from the given range.
Z
zengyawen 已提交
4

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


E
esterzhou 已提交
10
## Child Components
Z
zengyawen 已提交
11

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


## APIs

E
ester.zhou 已提交
17
DatePicker(options?: {start?: Date, end?: Date, selected?: Date})
Z
zengyawen 已提交
18

E
ester.zhou 已提交
19
Creates a date picker in the given date range.
Z
zengyawen 已提交
20

E
ester.zhou 已提交
21 22 23 24 25 26
**Parameters**
| Name| Type| Mandatory | Description|
| -------- | -------- | ------------- | -------- |
| start    | Date | No | Start date of the picker.<br>Default value: **Date('1970-1-1')**|
| end      | Date | No |   End date of the picker.<br>Default value: **Date('2100-12-31')**|
| selected | Date | No | Date of the selected item.<br>Default value: current system date |
Z
zengyawen 已提交
27 28 29 30


## Attributes

E
ester.zhou 已提交
31 32
| Name   | Type       | Description           |
| ------| -------------- | -------- |
E
ester.zhou 已提交
33
| lunar | boolean  | Whether to display the lunar calendar.<br>- **true**: Display the lunar calendar.<br>- **false**: Do not display the lunar calendar.<br>Default value: **false**|
Z
zengyawen 已提交
34 35 36 37


## Events

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

E
ester.zhou 已提交
42 43 44
## DatePickerResult

| Name| Type| Description|
E
ester.zhou 已提交
45
| -------- | -------- | -------- |
E
ester.zhou 已提交
46 47 48
| year | number | Year of the selected date.|
| month | number | Month of the selected date. The value ranges from 0 to 11. The value **0** indicates January, and **11** indicates December.|
| day | number | Day of the selected date.|
Z
zengyawen 已提交
49 50 51 52 53


## Example


E
ester.zhou 已提交
54 55
```ts
// xxx.ets
Z
zengyawen 已提交
56 57
@Entry
@Component
E
ester.zhou 已提交
58 59
struct DatePickerExample {
  @State isLunar: boolean = false
Z
zengyawen 已提交
60 61 62 63
  private selectedDate: Date = new Date('2021-08-08')

  build() {
    Column() {
E
ester.zhou 已提交
64 65 66 67 68
      Button('Switch Calendar')
        .margin({ top: 30 })
        .onClick(() => {
          this.isLunar = !this.isLunar
        })
Z
zengyawen 已提交
69 70
      DatePicker({
        start: new Date('1970-1-1'),
E
ester.zhou 已提交
71
        end: new Date('2100-1-1'),
E
ester.zhou 已提交
72
        selected: this.selectedDate
Z
zengyawen 已提交
73
      })
E
ester.zhou 已提交
74 75 76 77 78
        .lunar(this.isLunar)
        .onChange((value: DatePickerResult) => {
          this.selectedDate.setFullYear(value.year, value.month, value.day)
          console.info('select current date is: ' + JSON.stringify(value))
        })
Z
zengyawen 已提交
79 80 81 82 83 84

    }.width('100%')
  }
}
```

E
ester.zhou 已提交
85
![datePicker](figures/datePicker.gif)