diff --git a/src/main/ets/utils/TimeHelper.ts b/src/main/ets/utils/TimeHelper.ts index d5b5e07..5c17134 100644 --- a/src/main/ets/utils/TimeHelper.ts +++ b/src/main/ets/utils/TimeHelper.ts @@ -16,6 +16,7 @@ export class TimeHelper { private constructor() { } + /** * 获取当前时间戳 * @returns @@ -30,8 +31,9 @@ export class TimeHelper { * 'yyyy-MM-dd HH:mm:ss' */ static getTime(format: string = 'yyyy-MM-dd HH:mm:ss') { - return TimeHelper.formatDate(new Date(),format) + return TimeHelper.formatDate(new Date(), format) } + static formatDate(date: Date, format: string) { const replacements: { [key: string]: string } = { yyyy: date.getFullYear().toString(), @@ -43,4 +45,14 @@ export class TimeHelper { }; return format.replace(/yyyy|MM|dd|HH|mm|ss/g, matched => replacements[matched]); } + + /** + * 获取月份天数 + * @param year + * @param month + * @returns + */ + static getMonthDays(year: number, month: number) { + return new Date(year, month, 0).getDate() + } } diff --git a/src/main/ets/utils/compose/DatePickerView.ets b/src/main/ets/utils/compose/DatePickerView.ets index 7534b73..bf3a8a7 100644 --- a/src/main/ets/utils/compose/DatePickerView.ets +++ b/src/main/ets/utils/compose/DatePickerView.ets @@ -6,6 +6,9 @@ import { ToolsHelper } from '../ToolsHelper' export struct DatePickerView { @Prop option: DateDialogOptions | undefined = undefined @Prop dialogTag: string | undefined = undefined + @State showYear: boolean = false + @State showMonth: boolean = false + @State showDay: boolean = false //年份相关 @State startYear: number = 1970 @State endYear: number = 1970 @@ -26,7 +29,14 @@ export struct DatePickerView { if (!this.option?.type) { this.option!.type = 'YYYY-MM-DD' } - if (this.option!.type.indexOf('YYYY') > -1) { + this.checkYear() + this.checkMonth() + this.checkDays() + } + + checkYear() { + this.showYear = this.option!.type!.indexOf('YYYY') > -1 + if (this.showYear) { if (this.option?.startDate) { this.startYear = Number.parseInt(this.option.startDate?.split('-')[0]) } @@ -46,12 +56,11 @@ export struct DatePickerView { this.currentYear = this.years.indexOf(new Date().getFullYear().toString()) } } - this.checkMonth() - this.checkDays() } checkMonth() { - if (this.option!.type!.indexOf('MM') > -1) { + this.showMonth = this.option!.type!.indexOf('MM') > -1 + if (this.showMonth) { if (this.option?.startDate) { this.startMonth = Number.parseInt(this.option.startDate?.split('-')[1]) } @@ -80,7 +89,8 @@ export struct DatePickerView { } checkDays() { - if (this.option!.type!.indexOf('DD') > -1) { + this.showDay = this.option!.type!.indexOf('DD') > -1 + if (this.showDay) { if (this.option?.startDate) { this.startDay = Number.parseInt(this.option.startDate?.split('-')[2]) } @@ -135,15 +145,21 @@ export struct DatePickerView { .padding(15) Row() { - TextPicker({ range: this.years, selected: $$this.currentYear }) - .onChange((value: string | string[], index: number | number[]) => { - this.checkDays() - }).canLoop(false) - TextPicker({ range: this.months, selected: $$this.currentMonth }) - .onChange((value: string | string[], index: number | number[]) => { - this.checkDays() - }).canLoop(false) - TextPicker({ range: this.days, selected: $$this.currentDay }) + if (this.startYear) { + TextPicker({ range: this.years, selected: $$this.currentYear }) + .onChange((value: string | string[], index: number | number[]) => { + this.checkDays() + }).canLoop(false) + } + if (this.startMonth) { + TextPicker({ range: this.months, selected: $$this.currentMonth }) + .onChange((value: string | string[], index: number | number[]) => { + this.checkDays() + }).canLoop(false) + } + if (this.startDay) { + TextPicker({ range: this.days, selected: $$this.currentDay }) + } }.width('100%').justifyContent(FlexAlign.Center).alignItems(VerticalAlign.Center) }