中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
可以切换上一月(的一号)/下一月/切换到本月的今天 停留在某月时,可以切换选择日期。
微信扫码分享
// import { Lunar } from 'lunar-javascript' import dateClass from '../beans/DateClass' import dayjs from 'dayjs' @Entry @Component struct DatePickerDialogExample01 { weekList: Array<string> = ['一', '二', '三', '四', '五', '六', '日'] @State show: boolean = false @State selectDate: dayjs.Dayjs = dayjs() @State selectMonth: string = dayjs().format('YYYY年MM月') @State dataList: Array<dateClass> = [] @State monthParam: number = 0 //切换日历的参数 build() { Column() { Button('弹出或关闭日历') .height(36) .fontSize(20) .fontColor(Color.White) .onClick(() => { this.show = !this.show this.selectDate = dayjs() this.monthParam = 0 this.getDataList(this.monthParam) }) if (this.show) { Row() { Text(this.selectMonth) .fontSize(16) .fontColor(Color.White) Row() { Row() { Image($r('app.media.startIcon')) .width(24) .height(12) .backgroundColor(Color.Black) } .height('100%') .width(48) .alignItems(VerticalAlign.Center) .justifyContent(FlexAlign.Center) .onClick(() => { this.monthParam-- this.getDataList(this.monthParam) }) Row() { Image($r('app.media.startIcon')) .width(24) .height(12) .backgroundColor(Color.Black) } .height('100%') .width(48) .alignItems(VerticalAlign.Center) .justifyContent(FlexAlign.Center) .onClick(() => { this.monthParam++ this.getDataList(this.monthParam) }) } } .height(48) .width('100%') .justifyContent(FlexAlign.SpaceBetween) Row() { ForEach(this.weekList, (item: string) => { Text(item) .fontSize(16) Row() { ForEach(this.weekList, (item: string) => { Text(item) .fontSize(16) .fontColor(Color.White) .width(48) .textAlign(TextAlign.Center) }) } .width('100%') Grid() { ForEach(this.dataList, (item: dateClass) => { GridItem() { Column() { Text(item.day.toString()) .fontSize(16) .fontColor(Color.White) .width(48) .textAlign(TextAlign.Center) .opacity(item.isToMonth ? 1 : 0.4) Text(item.lunarDay) .fontSize(12) .fontColor(Color.White) .width(48) .textAlign(TextAlign.Center) .opacity(item.isToMonth ? 1 : 0.4) } .width(48) .height(48) .alignItems(HorizontalAlign.Center) .justifyContent(FlexAlign.Center) .backgroundColor(item.dayjsObj.format('YYYY-MM-DD') == dayjs().format('YYYY-MM-DD') ? '#007DFF' : Color.Black) .borderWidth(1) .borderColor(item.dayjsObj.format('YYYY-MM-DD') == this.selectDate.format('YYYY-MM-DD') ? '#007DFF' : Color.Black) .onClick(() => { this.selectDate = item.dayjsObj }) } }) } .height(288) .columnsTemplate('1fr 1fr 1fr 1fr 1fr 1fr 1fr') .rowsTemplate('1fr 1fr 1fr 1fr 1fr 1fr') .columnsGap(0) .rowsGap(0) } } .width('100%') .height('100%') .backgroundColor(Color.Black) .justifyContent(FlexAlign.Center) .padding({ left: 12, right: 12 }) } getDataList(param: number) { this.dataList = [] let firstDate = dayjs().add(param, 'month').startOf('month') // 当月的第一天 let afterDate = dayjs().add(param, 'month').endOf('month') // 当月的最后一天 this.selectMonth = firstDate.format('YYYY年MM月') let frontDay = 0 // 显示日历的第一天需要向前的天数 if (firstDate.day() == 0) { frontDay = 6 } else { frontDay = firstDate.day() - 1 } let showFirstDay = firstDate.subtract(frontDay, 'day') // 显示日历的第一天 for (let i = 0;i < 42; i++) { let dayjsObj = showFirstDay.add(i, 'day') let day = dayjsObj.date() let lunarDay = '' // if (Lunar.fromDate(dayjsObj.toDate()).getFestivals().length !== 0) { // 显示节日 // lunarDay = Lunar.fromDate(dayjsObj.toDate()).getFestivals()[0] // } else if (Lunar.fromDate(dayjsObj.toDate()).getJieQi() !== '') { // 显示节气 // lunarDay = Lunar.fromDate(dayjsObj.toDate()).getJieQi() // } else { // 显示农历日期 // lunarDay = Lunar.fromDate(dayjsObj.toDate()).getDayInChinese() // } let isToMonth = true if (dayjsObj.isBefore(firstDate) || dayjsObj.isAfter(afterDate)) { isToMonth = false } this.dataList.push(new dateClass(dayjsObj, day, lunarDay, isToMonth)) } // console.log('aboutToAppear',JSON.stringify(this.dataList)) } aboutToAppear() { this.getDataList(this.monthParam) } } // @ts-ignore import dayjs from 'dayjs' export default class DateClass { dayjsObj: dayjs.Dayjs // dayjs时间 day: number // 天 lunarDay: string // 农历 isToMonth: boolean // 是否当前月 constructor(dayjsObj: dayjs.Dayjs, day: number, lunarDay: string, isToMonth: boolean) { this.dayjsObj = dayjsObj this.day = day this.lunarDay = lunarDay this.isToMonth = isToMonth } }