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

3
> **NOTE**<br>
Z
zengyawen 已提交
4 5 6
> This component is supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.


E
ester.zhou 已提交
7
The **\<DatePicker>** component allows users to select a date from the given range.
Z
zengyawen 已提交
8 9 10 11


## Required Permissions

E
ester.zhou 已提交
12
No
Z
zengyawen 已提交
13 14


E
esterzhou 已提交
15
## Child Components
Z
zengyawen 已提交
16

E
ester.zhou 已提交
17
No
Z
zengyawen 已提交
18 19 20 21


## APIs

E
ester.zhou 已提交
22
DatePicker(options?: DatePickerOptions)
Z
zengyawen 已提交
23

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

E
ester.zhou 已提交
26
- options parameters
27
  | Name | Type | Mandatory | Default Value | Description |
Z
zengyawen 已提交
28
  | -------- | -------- | -------- | -------- | -------- |
29 30 31
  | start | Date | No| Date('1970-1-1') | Start date of the picker. |
  | end | Date | No| Date('2100-12-31') | End date of the picker. |
  | selected | Date | No| Current system date| Date of the selected item. |
Z
zengyawen 已提交
32 33 34 35


## Attributes

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


## Events

43
| Name | Description |
Z
zengyawen 已提交
44
| -------- | -------- |
45
| onChange(callback:&nbsp;(value:&nbsp;DatePickerResult)&nbsp;=&gt;&nbsp;void) | Invoked when a date is selected. |
Z
zengyawen 已提交
46

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


## Example


E
ester.zhou 已提交
58
### Date Picker Sample Code (With Lunar Calendar)
Z
zengyawen 已提交
59 60 61 62 63 64 65 66 67 68 69

```
@Entry
@Component
struct DatePickerExample01 {
  private selectedDate: Date = new Date('2021-08-08')

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


E
ester.zhou 已提交
83
### Date Picker Sample Code (No Lunar Calendar)
Z
zengyawen 已提交
84 85 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 107
        selected: this.selectedDate,
      })
      .lunar(false)
      .onChange((date: DatePickerResult) => {
        console.info('select current date is: ' + JSON.stringify(date))
      })
    }.width('100%')
  }
}
```