样式效果
image.png
TS 文件 dateTimePicker.ts
function withData(param: number): string {
return param < 10 ? '0' + param : '' + param
}
function getLoopArray(start: number, end: number, unit: string): string[] {
const array = []
for (let i = start; i <= end; i++) {
array.push(withData(i) + unit)
}
return array
}
export function getMonthDay(year: any, month: any, unit: string) {
const flag = year % 400 === 0 || (year % 4 === 0 && year % 100 !== 0)
let array: string[] | string = []
switch (month) {
case '01月':
case '03月':
case '05月':
case '07月':
case '08月':
case '10月':
case '12月':
array = getLoopArray(1, 31, unit)
break
case '04月':
case '06月':
case '09月':
case '11月':
array = getLoopArray(1, 30, unit)
break
case '02月':
array = flag ? getLoopArray(1, 29, unit) : getLoopArray(1, 28, unit)
break
default:
array = '月份格式不正确,请重新输入!'
}
return array
}
function getNewDateArry() {
// 当前时间的处理
const newDate = new Date()
const year = withData(newDate.getFullYear()) + '年'
const mont = withData(newDate.getMonth() + 1) + '月'
const date = withData(newDate.getDate()) + '日'
const hour = withData(newDate.getHours()) + '时'
const minu = withData(newDate.getMinutes()) + '分'
return [year, mont, date, hour, minu]
}
export function dateTimePicker(startYear: any, endYear: any, date: any): any {
// 返回默认显示的数组和联动数组的声明
const dateTime: any = []
const start = startYear || 1978
const end = endYear || 2100
// 默认开始显示数据
const defaultDate = date ? [...date.split(' ')[0].split('-'), ...date.split(' ')[1].split(':')] : getNewDateArry()
// 处理联动列表数据
const dateTimeArray = [
getLoopArray(start, end, '年'),
getLoopArray(1, 12, '月'),
getMonthDay(defaultDate[0], defaultDate[1], '日'),
getLoopArray(0, 23, '时'),
getLoopArray(0, 59, '分')
]
dateTimeArray.forEach((item, index) => {
dateTime.push(item.indexOf(defaultDate[index]))
})
return {
dateTimeArray: dateTimeArray,
dateTime: dateTime
}
}
wxml
<picker mode="multiSelector" value="{{ dateTime }}" bindchange="changeDateStartTime" bindcolumnchange="changeDateTimeColumn" range="{{ dateTimeArray }}">
</picker>
引用
import { dateTimePicker, getMonthDay } from './dateTimePicker'
data:{
dateTime: null,
dateTimeArray: null,
},
reLoad() {
/**
* 获取当前年月日
*/
getTime() {
// 获取完整的年月日 时分秒,以及默认显示的数组
var obj = dateTimePicker(this.data.startYear, this.data.endYear, '')
var obj1 = dateTimePicker(this.data.startYear, this.data.endYear, '')
this.setData({
dateTime: obj.dateTime,
dateTimeArray: obj.dateTimeArray,
dateTimeArray1: obj1.dateTimeArray,
dateTime1: obj1.dateTime
})
}
}
//调用方法
/**
* 时间选择器
*/
changeDateTimeColumn(e) {
const arr = this.data.dateTime
const dateArr = this.data.dateTimeArray
arr[e.detail.column] = e.detail.value
dateArr[2] = getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]], '日')
this.setData({
dateTimeArray: dateArr
})
},
/**
* 选中时间
*/
changeDateStartTime(e) {
const dateTimeArray = this.data.dateTimeArray
const dateTime = e.detail.value
const year = dateTimeArray[0][dateTime[0]].substring(0, 4)
const month = dateTimeArray[1][dateTime[1]].substring(0, 2)
const day = dateTimeArray[2][dateTime[2]].substring(0, 2)
const hour = dateTimeArray[3][dateTime[3]].substring(0, 2)
const min = dateTimeArray[4][dateTime[4]].substring(0, 2)
const dates = year + '-' + month + '-' + day + ' ' + hour + ':' + min + ':00'
const startTime = this.transferTime(dates)
const timestamp = new Date().getTime()
if (startTime - timestamp < 0) {
tip.showToast({
title: '开始时间不能小于当前时间'
})
return
}
this.setData({
'form.startTime': dates
})
}
网友评论