ts-methods-datepicker-dialog.md 3.8 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
**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.|
E
ester.zhou 已提交
24 25 26 27 28
| showTime<sup>10+</sup> | boolean | No| false | Whether to display the time item.|
| useMilitaryTime<sup>10+</sup> | boolean | No| false | Whether to display time in 24-hour format.|
| disappearTextStyle<sup>10+</sup> | [PickerTextStyle](ts-basic-components-datepicker.md#pickertextstyle10) | No| - | Font color, font size, and font width for the top and bottom items.|
| textStyle<sup>10+</sup> | [PickerTextStyle](ts-basic-components-datepicker.md#pickertextstyle10) | No| - | Font color, font size, and font width of all items except the top, bottom, and selected items.|
| selectedTextStyle<sup>10+</sup> | [PickerTextStyle](ts-basic-components-datepicker.md#pickertextstyle10) | No| - | Font color, font size, and font width of the selected item.|
E
esterzhou 已提交
29 30 31
| 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 已提交
32 33 34

## Example

E
ester.zhou 已提交
35 36
```ts
// xxx.ets
E
esterzhou 已提交
37 38
@Entry
@Component
E
esterzhou 已提交
39 40
struct DatePickerDialogExample {
  selectedDate: Date = new Date("2010-1-1")
E
esterzhou 已提交
41 42

  build() {
E
esterzhou 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
    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 已提交
63 64
        })

E
esterzhou 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
      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 已提交
84
        })
E
esterzhou 已提交
85
    }.width('100%')
E
esterzhou 已提交
86 87 88
  }
}
```