1e41f4b71Sopenharmony_ci# DatePicker 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe **DatePicker** component allows users to select a date from the given range. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **NOTE** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> This component is supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci## Child Components 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ciNot supported 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci## APIs 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ciDatePicker(options?: DatePickerOptions) 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ciCreates a date picker in the given date range. 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci**Parameters** 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 28e41f4b71Sopenharmony_ci| ------- | ----------------------------------------------- | ---- | -------------------------- | 29e41f4b71Sopenharmony_ci| options | [DatePickerOptions](#datepickeroptions) | No | Parameters of the date picker.| 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci## DatePickerOptions 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci| Name | Type| Mandatory| Description | 38e41f4b71Sopenharmony_ci| -------- | ---- | ---- | ------------------------------------------------------------ | 39e41f4b71Sopenharmony_ci| start | Date | No | Start date of the picker.<br>Default value: **Date('1970-1-1')** | 40e41f4b71Sopenharmony_ci| end | Date | No | End date of the picker.<br>Default value: **Date('2100-12-31')** | 41e41f4b71Sopenharmony_ci| selected | Date | No | Date of the selected item.<br>Default value: current system date<br>Since API version 10, this parameter supports two-way binding through [$$](../../../quick-start/arkts-two-way-sync.md).| 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci**Handling in the case of exceptions** 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci| Exception | Result | 46e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | 47e41f4b71Sopenharmony_ci| The start date is later than the end date, and the selected date is not set. | The start date, end date, and selected date are set to the default values. | 48e41f4b71Sopenharmony_ci| The start date is later than the end date, and the selected date is earlier than the default start date. | The start date and end date are set to the default values, and the selected date is set to the default start date. | 49e41f4b71Sopenharmony_ci| The start date is later than the end date, and the selected date is later than the default end date. | The start date and end date are set to the default values, and the selected date is set to the default end date. | 50e41f4b71Sopenharmony_ci| The start date is later than the end date, and the selected date is within the range of the default start date and end date. | The start date and end date are set to the default values, and the selected date is set to the specified value.| 51e41f4b71Sopenharmony_ci| The selected date is earlier than the start date. | The start date is set to the selected date. | 52e41f4b71Sopenharmony_ci| The selected date is later than the end date. | The selected date is set to the end date. | 53e41f4b71Sopenharmony_ci| The start date is later than the current system date, and the selected date is not set. | The start date is set to the selected date. | 54e41f4b71Sopenharmony_ci| The end date is earlier than the current system date, and the selected date is not set. | The end date is set to the selected date. | 55e41f4b71Sopenharmony_ci| The set date is in invalid format, for example, **'1999-13-32'**. | The default value is used. | 56e41f4b71Sopenharmony_ci| The start date or end date is earlier than the earliest date in the valid date range. | The start date or end date is set to the default state date.| 57e41f4b71Sopenharmony_ci| The start date or end date is later than the latest date in the valid date range. | The start date or end date is set to the default end date.| 58e41f4b71Sopenharmony_ci| Both the start date and end date are earlier than the earliest date in the valid date range.| The start date and end date are set to the earliest date in the valid date range.| 59e41f4b71Sopenharmony_ci| Both the start date and end date are later than the latest date in the valid date range.| The start date and end date are set to the latest date in the valid date range.| 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ciThe valid date range is from 1900-1-31 to 2100-12-31. 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ciThe exception detection and handling with the selected date comes after that with the start date and end date. 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci## Attributes 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ciIn addition to the [universal attributes](ts-universal-attributes-size.md), the following attributes are supported. 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci### lunar 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_cilunar(value: boolean) 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ciSpecifies whether to display the lunar calendar. 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ci**Parameters** 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 82e41f4b71Sopenharmony_ci| ------ | ------- | ---- | ------------------------------------------------------------ | 83e41f4b71Sopenharmony_ci| value | boolean | Yes | Whether to display the lunar calendar.<br>- **true**: Display the lunar calendar.<br>- **false**: Do not display the lunar calendar.<br>Default value: **false**| 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ci### disappearTextStyle<sup>10+</sup> 86e41f4b71Sopenharmony_ci 87e41f4b71Sopenharmony_cidisappearTextStyle(value: PickerTextStyle) 88e41f4b71Sopenharmony_ci 89e41f4b71Sopenharmony_ciSets the text style for the top and bottom items. 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_ci**Parameters** 96e41f4b71Sopenharmony_ci 97e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 98e41f4b71Sopenharmony_ci| ------ | --------------------------------------------- | ---- | ------------------------------------------------------------ | 99e41f4b71Sopenharmony_ci| value | [PickerTextStyle](#pickertextstyle10) | Yes | Font color, font size, and font weight of the top and bottom items.<br>Default value:<br>{<br>color: '#ff182431',<br>font: {<br>size: '14fp', <br>weight: FontWeight.Regular<br>}<br>} | 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_ci### textStyle<sup>10+</sup> 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_citextStyle(value: PickerTextStyle) 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ciSets the text style for all items except the top, bottom, and selected items. 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ci**Parameters** 112e41f4b71Sopenharmony_ci 113e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 114e41f4b71Sopenharmony_ci| ------ | --------------------------------------------- | ---- | ------------------------------------------------------------ | 115e41f4b71Sopenharmony_ci| value | [PickerTextStyle](#pickertextstyle10) | Yes | Font color, font size, and font weight of all items except the top, bottom, and selected items.<br>Default value:<br>{<br>color: '#ff182431',<br>font: {<br>size: '16fp', <br>weight: FontWeight.Regular<br>}<br>} | 116e41f4b71Sopenharmony_ci 117e41f4b71Sopenharmony_ci### selectedTextStyle<sup>10+</sup> 118e41f4b71Sopenharmony_ci 119e41f4b71Sopenharmony_ciselectedTextStyle(value: PickerTextStyle) 120e41f4b71Sopenharmony_ci 121e41f4b71Sopenharmony_ciSets the text style for the selected item. 122e41f4b71Sopenharmony_ci 123e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ci**Parameters** 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 130e41f4b71Sopenharmony_ci| ------ | --------------------------------------------- | ---- | ------------------------------------------------------------ | 131e41f4b71Sopenharmony_ci| value | [PickerTextStyle](#pickertextstyle10) | Yes | Font color, font size, and font weight of the selected item.<br>Default value:<br>{<br>color: '#ff007dff',<br>font: {<br>size: '20vp', <br>weight: FontWeight.Medium<br>}<br>} | 132e41f4b71Sopenharmony_ci 133e41f4b71Sopenharmony_ci## PickerTextStyle<sup>10+</sup> 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 138e41f4b71Sopenharmony_ci| ----- | ---------------------------------------- | ---- | ------------------------- | 139e41f4b71Sopenharmony_ci| color | [ResourceColor](ts-types.md#resourcecolor) | No | Font color. | 140e41f4b71Sopenharmony_ci| font | [Font](ts-types.md#font) | No | Text style. Only the font size and font weight are supported. | 141e41f4b71Sopenharmony_ci 142e41f4b71Sopenharmony_ci## Events 143e41f4b71Sopenharmony_ci 144e41f4b71Sopenharmony_ciIn addition to the [universal events](ts-universal-events-click.md), the following events are supported. 145e41f4b71Sopenharmony_ci 146e41f4b71Sopenharmony_ci### onChange<sup>(deprecated)</sup> 147e41f4b71Sopenharmony_ci 148e41f4b71Sopenharmony_cionChange(callback: (value: DatePickerResult) => void) 149e41f4b71Sopenharmony_ci 150e41f4b71Sopenharmony_ciTriggered when a date is selected. 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_ciThis API is supported since API version 8 and deprecated since API version 10. You are advised to use [onDateChange](#ondatechange10) instead. 153e41f4b71Sopenharmony_ci 154e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 155e41f4b71Sopenharmony_ci 156e41f4b71Sopenharmony_ci**Parameters** 157e41f4b71Sopenharmony_ci 158e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 159e41f4b71Sopenharmony_ci| ------ | --------------------------------------------- | ---- | ---------------- | 160e41f4b71Sopenharmony_ci| value | [DatePickerResult](#datepickerresult) | Yes | Selected time.| 161e41f4b71Sopenharmony_ci 162e41f4b71Sopenharmony_ci### onDateChange<sup>10+</sup> 163e41f4b71Sopenharmony_ci 164e41f4b71Sopenharmony_cionDateChange(callback: (value: Date) => void) 165e41f4b71Sopenharmony_ci 166e41f4b71Sopenharmony_ciTriggered when a date is selected. 167e41f4b71Sopenharmony_ci 168e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 169e41f4b71Sopenharmony_ci 170e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 171e41f4b71Sopenharmony_ci 172e41f4b71Sopenharmony_ci**Parameters** 173e41f4b71Sopenharmony_ci 174e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description | 175e41f4b71Sopenharmony_ci| ------ | ---- | ---- | ------------------------------------------------------------ | 176e41f4b71Sopenharmony_ci| value | Date | Yes | Selected time, where the year, month, and day portions are subject to the selection, the hour and minute portions are subject to the current system time, and the second portion is always **00**.| 177e41f4b71Sopenharmony_ci 178e41f4b71Sopenharmony_ci## DatePickerResult 179e41f4b71Sopenharmony_ci 180e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 181e41f4b71Sopenharmony_ci 182e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 183e41f4b71Sopenharmony_ci 184e41f4b71Sopenharmony_ci| Name | Type | Read Only| Optional| Description | 185e41f4b71Sopenharmony_ci| ----- | ------ | ---- | ---- | ------------------------------------------ | 186e41f4b71Sopenharmony_ci| year | number | No | No | Year of the selected date. | 187e41f4b71Sopenharmony_ci| month | number | No | No | Month of the selected date. The value ranges from 0 to 11. The value **0** indicates January, and **11** indicates December.| 188e41f4b71Sopenharmony_ci| day | number | No | No | Day of the selected date. | 189e41f4b71Sopenharmony_ci 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_ci## Example 192e41f4b71Sopenharmony_ci 193e41f4b71Sopenharmony_ci 194e41f4b71Sopenharmony_ci```ts 195e41f4b71Sopenharmony_ci// xxx.ets 196e41f4b71Sopenharmony_ci@Entry 197e41f4b71Sopenharmony_ci@Component 198e41f4b71Sopenharmony_cistruct DatePickerExample { 199e41f4b71Sopenharmony_ci @State isLunar: boolean = false 200e41f4b71Sopenharmony_ci private selectedDate: Date = new Date('2021-08-08') 201e41f4b71Sopenharmony_ci 202e41f4b71Sopenharmony_ci build() { 203e41f4b71Sopenharmony_ci Column() { 204e41f4b71Sopenharmony_ci Button('Switch Calendar') 205e41f4b71Sopenharmony_ci .margin({ top: 30, bottom: 30 }) 206e41f4b71Sopenharmony_ci .onClick(() => { 207e41f4b71Sopenharmony_ci this.isLunar = !this.isLunar 208e41f4b71Sopenharmony_ci }) 209e41f4b71Sopenharmony_ci DatePicker({ 210e41f4b71Sopenharmony_ci start: new Date('1970-1-1'), 211e41f4b71Sopenharmony_ci end: new Date('2100-1-1'), 212e41f4b71Sopenharmony_ci selected: this.selectedDate 213e41f4b71Sopenharmony_ci }) 214e41f4b71Sopenharmony_ci .disappearTextStyle({color: Color.Gray, font: {size: '16fp', weight: FontWeight.Bold}}) 215e41f4b71Sopenharmony_ci .textStyle({color: '#ff182431', font: {size: '18fp', weight: FontWeight.Normal}}) 216e41f4b71Sopenharmony_ci .selectedTextStyle({color: '#ff0000FF', font: {size: '26fp', weight: FontWeight.Regular}}) 217e41f4b71Sopenharmony_ci .lunar(this.isLunar) 218e41f4b71Sopenharmony_ci .onDateChange((value: Date) => { 219e41f4b71Sopenharmony_ci this.selectedDate = value 220e41f4b71Sopenharmony_ci console.info('select current date is: ' + value.toString()) 221e41f4b71Sopenharmony_ci }) 222e41f4b71Sopenharmony_ci 223e41f4b71Sopenharmony_ci }.width('100%') 224e41f4b71Sopenharmony_ci } 225e41f4b71Sopenharmony_ci} 226e41f4b71Sopenharmony_ci``` 227e41f4b71Sopenharmony_ci 228e41f4b71Sopenharmony_ci 229