ts-basic-components-datepicker.md 2.6 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 10 11




E
esterzhou 已提交
12
## Child Components
Z
zengyawen 已提交
13

E
ester.zhou 已提交
14
Not supported
Z
zengyawen 已提交
15 16 17 18


## APIs

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

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

E
ester.zhou 已提交
23 24 25 26 27 28
**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 已提交
29 30 31 32


## Attributes

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


## Events

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

E
ester.zhou 已提交
44 45 46
## DatePickerResult

| Name| Type| Description|
E
ester.zhou 已提交
47
| -------- | -------- | -------- |
E
ester.zhou 已提交
48 49 50
| 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 已提交
51 52 53 54 55


## Example


E
ester.zhou 已提交
56
### Date Picker Sample Code (With Lunar Calendar)
Z
zengyawen 已提交
57

E
ester.zhou 已提交
58 59
```ts
// xxx.ets
Z
zengyawen 已提交
60 61 62 63 64 65 66 67 68
@Entry
@Component
struct DatePickerExample01 {
  private selectedDate: Date = new Date('2021-08-08')

  build() {
    Column() {
      DatePicker({
        start: new Date('1970-1-1'),
E
ester.zhou 已提交
69
        end: new Date('2100-1-1'),
Z
zengyawen 已提交
70 71 72 73 74 75 76 77 78 79 80 81
        selected: this.selectedDate,
      })
      .lunar(true)
      .onChange((date: DatePickerResult) => {
        console.info('select current date is: ' + JSON.stringify(date))
      })
    }.width('100%')
  }
}
```


E
ester.zhou 已提交
82
### Date Picker Sample Code (No Lunar Calendar)
Z
zengyawen 已提交
83

E
ester.zhou 已提交
84 85
```ts
// xxx.ets
Z
zengyawen 已提交
86 87 88 89 90 91 92 93 94
@Entry
@Component
struct DatePickerExample02 {
  private selectedDate: Date = new Date('2021-08-08')

  build() {
    Column() {
      DatePicker({
        start: new Date('1970-1-1'),
E
ester.zhou 已提交
95
        end: new Date('2100-1-1'),
Z
zengyawen 已提交
96 97 98 99 100 101 102 103 104 105 106
        selected: this.selectedDate,
      })
      .lunar(false)
      .onChange((date: DatePickerResult) => {
        console.info('select current date is: ' + JSON.stringify(date))
      })
    }.width('100%')
  }
}
```

E
ester.zhou 已提交
107
![en-us_image_0000001251092975](figures/en-us_image_0000001251092975.png)