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


E
esterzhou 已提交
4
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
Z
zengyawen 已提交
5 6 7 8 9 10 11 12 13 14 15
> This component is supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.


The **<DatePicker>** component allows users to select date and time.


## Required Permissions

None


E
esterzhou 已提交
16
## Child Components
Z
zengyawen 已提交
17 18 19 20 21 22 23 24 25 26 27

None


## APIs

DatePicker(value:{start?: Date, end?: Date, selected?: Date, type?: DatePickerType})

Creates a date picker that allows users to select a date or time within the specified range.

- Parameters
E
esterzhou 已提交
28
    | Name | Type | Mandatory | Default Value | Description |
Z
zengyawen 已提交
29
  | -------- | -------- | -------- | -------- | -------- |
E
esterzhou 已提交
30 31 32 33
  | start | Date | No | Date('1970-1-1') | Start date of the date picker. |
  | end | Date | No | Date('2100-12-31') | End date of the date picker. |
  | selected | Date | No | Current system date or time | Selected date when **type** is set to **DatePickerType.Date** and selected time when **type** is set to **DatePickerType.Time**. |
  | type | DatePickerType | No | DatePickerType.Date | Picker type, which can be date picker and time picker. The date picker is used by default. |
Z
zengyawen 已提交
34 35

- DatePickerType enums
E
esterzhou 已提交
36
    | Name | Description |
Z
zengyawen 已提交
37
  | -------- | -------- |
E
esterzhou 已提交
38 39
  | Date | Date picker. |
  | Time | Time picker. |
Z
zengyawen 已提交
40 41 42 43


## Attributes

E
esterzhou 已提交
44
| Name | Type | Default Value | Description |
Z
zengyawen 已提交
45
| -------- | -------- | -------- |-------- |
E
esterzhou 已提交
46 47
| lunar | boolean | false | Whether to display the lunar calendar.<br/>- **true**: The lunar calendar is displayed.<br/>- **false**: The lunar calendar is not displayed. |
| useMilitaryTime | boolean | false | Whether the display time is in 24-hour format. The value cannot be dynamically modified. |
Z
zengyawen 已提交
48 49 50 51


## Events

E
esterzhou 已提交
52
| Name | Description |
Z
zengyawen 已提交
53
| -------- | -------- |
E
esterzhou 已提交
54
| onChange(callback: (value: DatePickerResult) =&gt; void) | This event is triggered when a date or time is selected. |
Z
zengyawen 已提交
55

E
esterzhou 已提交
56 57
- DatePickerResult
    | Name | Type | Description |
Z
zengyawen 已提交
58
  | -------- | -------- | -------- |
E
esterzhou 已提交
59 60 61 62 63
  | year | number | Year of the selected date (when **type** is **DatePickerType.Date**). |
  | month | number | Month of the selected date (when **type** is **DatePickerType.Date**). |
  | day | number | Date of the selected date (when **type** is **DatePickerType.Date**). |
  | hour | number | Hour portion of the selected time (when **type** is **DatePickerType.Time**). |
  | minute | number | Minute portion of the selected time (when **type** is **DatePickerType.Time**). |
Z
zengyawen 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150


## Example


### Date Picker (with Lunar Calendar)


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

  build() {
    Column() {
      DatePicker({
        start: new Date('1970-1-1'),
        end: new Date('2200-1-1'),
        selected: this.selectedDate,
        type: DatePickerType.Date
      })
      .lunar(true)
      .onChange((date: DatePickerResult) => {
        console.info('select current date is: ' + JSON.stringify(date))
      })
    }.width('100%')
  }
}
```


### Date Picker (Without Lunar Calendar)


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

  build() {
    Column() {
      DatePicker({
        start: new Date('1970-1-1'),
        end: new Date('2200-1-1'),
        selected: this.selectedDate,
        type: DatePickerType.Date
      })
      .lunar(false)
      .onChange((date: DatePickerResult) => {
        console.info('select current date is: ' + JSON.stringify(date))
      })
    }.width('100%')
  }
}
```



### Time Picker


```
@Entry
@Component
struct DatePickerExample03 {
  private selectedTime: Date = new Date('2021-9-29 08:00:00')

  build() {
    Column() {
      DatePicker({
        start: new Date('00:00:00'),
        end: new Date('23:59:59'),
        selected: this.selectedTime,
        type: DatePickerType.Time
      })
      .useMilitaryTime(true)
      .onChange((date: DatePickerResult) => {
        console.info('select current date is: ' + JSON.stringify(date))
      })
    }.width('100%')
  }
}
```

![en-us_image_0000001256858401](figures/en-us_image_0000001256858401.gif)