Component({ /** * 组件的属性列表 */ properties: { }, /** * 组件的初始数据 */ data: { dateTimeArray1: null, dateTime1: null, startYear: null, endYear: null, }, ready() { this.initTime() }, /** * 组件的方法列表 */ methods: { initTime() { let that = this // 获取完整的年月日 时分秒,以及默认显示的数组 let obj = this.dateTimePicker(this.data.startYear, this.data.endYear) let obj1 = this.dateTimePicker(this.data.startYear, this.data.endYear) // 精确到分的处理,将数组的秒去掉 //let lastArray = obj1.dateTimeArray.pop(); //let lastTime = obj1.dateTime.pop(); console.log('obj1', obj1) this.setData({ dateTimeArray1: obj1.dateTimeArray, dateTime1: obj1.dateTime }); }, // 选择日期时间 changeDateTime1(e) { this.setData({ dateTime1: e.detail.value }); this.triggerEvent('change', {dateTimeArray: this.data.dateTimeArray1, dateTime: e.detail.value}) }, changeDateTimeColumn1(e) { let arr = this.data.dateTime1, dateArr = this.data.dateTimeArray1 arr[e.detail.column] = e.detail.value dateArr[2] = this.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]) this.setData({ dateTimeArray1: dateArr, dateTime1: arr }); this.triggerEvent('columnchange', {dateTimeArray: dateArr, dateTime: arr}) }, withData(param) { return param < 10 ? '0' + param : '' + param }, getLoopArray(st, ed) { let start = st || 0 let end = ed || 1 let array = [] for (let i = start; i <= end; i++) { array.push(this.withData(i)) } return array }, getMonthDay(year, month) { let flag = year % 400 == 0 || (year % 4 == 0 && year % 100 != 0), array = null switch (month) { case '01': case '03': case '05': case '07': case '08': case '10': case '12': array = this.getLoopArray(1, 31) break; case '04': case '06': case '09': case '11': array = this.getLoopArray(1, 30) break; case '02': array = flag ? this.getLoopArray(1, 29) : this.getLoopArray(1, 28) break; default: array = '月份格式不正确,请重新输入!' } return array; }, getNewDateArry() { // 当前时间的处理 let newDate = new Date(); let year = this.withData(newDate.getFullYear()), mont = this.withData(newDate.getMonth() + 1), date = this.withData(newDate.getDate()), hour = this.withData(newDate.getHours()), minu = this.withData(newDate.getMinutes()), seco = this.withData(newDate.getSeconds()) return [year, mont, date, hour, minu, seco] // return [year, mont, date, hour, minu]; }, dateTimePicker(startYear, endYear, date) { // 返回默认显示的数组和联动数组的声明 let dateTime = [], dateTimeArray = [[], [], [], [], []] let start = startYear || 1978 let end = endYear || 2100 // 默认开始显示数据 let defaultDate = date ? [...date.split(' ')[0].split('-'), ...date.split(' ')[1].split(':')] : this.getNewDateArry() // 处理联动列表数据 /*年月日 时分秒*/ dateTimeArray[0] = this.getLoopArray(start, end) dateTimeArray[1] = this.getLoopArray(1, 12) dateTimeArray[2] = this.getMonthDay(defaultDate[0], defaultDate[1]) dateTimeArray[3] = this.getLoopArray(0, 23) dateTimeArray[4] = this.getLoopArray(0, 59) dateTimeArray[5] = this.getLoopArray(0, 59) dateTimeArray.forEach((current, index) => { dateTime.push(current.indexOf(defaultDate[index])) }) return { dateTimeArray: dateTimeArray, dateTime: dateTime } }, } })