ts-methods-datepicker-dialog.md 3.1 KB
Newer Older
E
esterzhou 已提交
1 2
# Date Picker Dialog Box

E
ester.zhou 已提交
3 4 5 6
A date picker dialog box is a dialog box that allows users to select a date from the given range.

>  **NOTE**
>
E
esterzhou 已提交
7 8
> The APIs of this module are supported since API version 9. Updates will be marked with a superscript to indicate their earliest API version.

E
esterzhou 已提交
9 10 11

## DatePickerDialog.show

E
ester.zhou 已提交
12
show(options?: DatePickerDialogOptions)
E
esterzhou 已提交
13

E
ester.zhou 已提交
14
Shows a date picker dialog box.
E
esterzhou 已提交
15

E
esterzhou 已提交
16 17 18 19 20 21 22 23 24 25 26
**DatePickerDialogOptions**

| Name| Type| Mandatory| Default Value| Description|
| -------- | -------- | -------- | -------- | -------- |
| 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| Selected date.|
| lunar | boolean | No| false | Whether to display the lunar calendar.|
| onAccept | (value: [DatePickerResult](ts-basic-components-datepicker.md#DatePickerResult)) => void | No| - | Callback invoked when the OK button in the dialog box is clicked.|
| onCancel | () => void | No| - | Callback invoked when the Cancel button in the dialog box is clicked.|
| onChange | (value: [DatePickerResult](ts-basic-components-datepicker.md#DatePickerResult)) => void | No| - | Callback invoked when the selected item in the picker changes.|
E
esterzhou 已提交
27 28 29

## Example

E
ester.zhou 已提交
30 31
```ts
// xxx.ets
E
esterzhou 已提交
32 33
@Entry
@Component
E
esterzhou 已提交
34 35
struct DatePickerDialogExample {
  selectedDate: Date = new Date("2010-1-1")
E
esterzhou 已提交
36 37

  build() {
E
esterzhou 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
    Column() {
      Button("DatePickerDialog")
        .margin(20)
        .onClick(() => {
          DatePickerDialog.show({
            start: new Date("2000-1-1"),
            end: new Date("2100-12-31"),
            selected: this.selectedDate,
            onAccept: (value: DatePickerResult) => {
              // 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.
              this.selectedDate.setFullYear(value.year, value.month, value.day)
              console.info("DatePickerDialog:onAccept()" + JSON.stringify(value))
            },
            onCancel: () => {
              console.info("DatePickerDialog:onCancel()")
            },
            onChange: (value: DatePickerResult) => {
              console.info("DatePickerDialog:onChange()" + JSON.stringify(value))
            }
          })
E
esterzhou 已提交
58 59
        })

E
esterzhou 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
      Button("Lunar DatePickerDialog")
        .margin(20)
        .onClick(() => {
          DatePickerDialog.show({
            start: new Date("2000-1-1"),
            end: new Date("2100-12-31"),
            selected: this.selectedDate,
            lunar: true,
            onAccept: (value: DatePickerResult) => {
              this.selectedDate.setFullYear(value.year, value.month, value.day)
              console.info("DatePickerDialog:onAccept()" + JSON.stringify(value))
            },
            onCancel: () => {
              console.info("DatePickerDialog:onCancel()")
            },
            onChange: (value: DatePickerResult) => {
              console.info("DatePickerDialog:onChange()" + JSON.stringify(value))
            }
          })
E
esterzhou 已提交
79
        })
E
esterzhou 已提交
80
    }.width('100%')
E
esterzhou 已提交
81 82 83
  }
}
```