• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Date Picker Dialog Box
2
3A date picker dialog box is a dialog box that allows users to select a date from the given range.
4
5>  **NOTE**
6>
7> The APIs of this module are supported since API version 9. Updates will be marked with a superscript to indicate their earliest API version.
8
9
10## DatePickerDialog.show
11
12show(options?: DatePickerDialogOptions)
13
14Shows a date picker dialog box.
15
16**DatePickerDialogOptions**
17
18| Name| Type| Mandatory| Default Value| Description|
19| -------- | -------- | -------- | -------- | -------- |
20| start | Date | No| Date('1970-1-1') | Start date of the picker.|
21| end | Date | No| Date('2100-12-31') | End date of the picker.|
22| selected | Date | No| Current system date| Selected date.|
23| lunar | boolean | No| false | Whether to display the lunar calendar.|
24| onAccept | (value: [DatePickerResult](ts-basic-components-datepicker.md#DatePickerResult)) => void | No| - | Callback invoked when the OK button in the dialog box is clicked.|
25| onCancel | () => void | No| - | Callback invoked when the Cancel button in the dialog box is clicked.|
26| onChange | (value: [DatePickerResult](ts-basic-components-datepicker.md#DatePickerResult)) => void | No| - | Callback invoked when the selected item in the picker changes.|
27
28## Example
29
30```ts
31// xxx.ets
32@Entry
33@Component
34struct DatePickerDialogExample {
35  selectedDate: Date = new Date("2010-1-1")
36
37  build() {
38    Column() {
39      Button("DatePickerDialog")
40        .margin(20)
41        .onClick(() => {
42          DatePickerDialog.show({
43            start: new Date("2000-1-1"),
44            end: new Date("2100-12-31"),
45            selected: this.selectedDate,
46            onAccept: (value: DatePickerResult) => {
47              // Use the setFullYear method to set the date when the OK button is touched. In this way, when the date picker dialog box is displayed again, the selected date is the date last confirmed.
48              this.selectedDate.setFullYear(value.year, value.month, value.day)
49              console.info("DatePickerDialog:onAccept()" + JSON.stringify(value))
50            },
51            onCancel: () => {
52              console.info("DatePickerDialog:onCancel()")
53            },
54            onChange: (value: DatePickerResult) => {
55              console.info("DatePickerDialog:onChange()" + JSON.stringify(value))
56            }
57          })
58        })
59
60      Button("Lunar DatePickerDialog")
61        .margin(20)
62        .onClick(() => {
63          DatePickerDialog.show({
64            start: new Date("2000-1-1"),
65            end: new Date("2100-12-31"),
66            selected: this.selectedDate,
67            lunar: true,
68            onAccept: (value: DatePickerResult) => {
69              this.selectedDate.setFullYear(value.year, value.month, value.day)
70              console.info("DatePickerDialog:onAccept()" + JSON.stringify(value))
71            },
72            onCancel: () => {
73              console.info("DatePickerDialog:onCancel()")
74            },
75            onChange: (value: DatePickerResult) => {
76              console.info("DatePickerDialog:onChange()" + JSON.stringify(value))
77            }
78          })
79        })
80    }.width('100%')
81  }
82}
83```
84