本文实例为大家分享了uniapp实现日期时间选择器的具体代码,供大家参考,具体内容如下
由于项目需求需要,尝试过使用Vant组件库,但是一直出现问题,插件市场的插件又不太符合需求,使用就查了相关资料,最终捣鼓的效果如下:
首先现在根目录下创建util文件夹放dateTimePicker.js
dateTimePicker.js
function withData(param){
return param < 10 ? '0' + param : '' + param;
}
function getLoopArray(start,end){
var start = start || 0;
var end = end || 1;
var array = [];
for (var i = start; i <= end; i++) {
array.push(withData(i));
}
return array;
}
function getMonthDay(year,month){
var 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 = getLoopArray(1, 31)
break;
case '04':
case '06':
case '09':
case '11':
array = getLoopArray(1, 30)
break;
case '02':
array = flag ? getLoopArray(1, 29) : getLoopArray(1, 28)
break;
default:
array = '月份格式不正确,请重新输入!'
}
return array;
}
function getNewDateArry(){
// 当前时间的处理 年月日时分秒
var newDate = new Date();
var year = withData(newDate.getFullYear()),
mont = withData(newDate.getMonth() + 1),
date = withData(newDate.getDate()),
hour = withData(newDate.getHours()),
minu = withData(newDate.getMinutes());
// seco = withData(newDate.getSeconds());
return [year, mont, date, hour, minu];
}
function dateTimePicker(startYear,endYear,date) {
// 返回默认显示的数组和联动数组的声明
var dateTime = [], dateTimeArray = [[],[],[],[],[]];
var start = startYear || 1978;
var end = endYear || 2100;
//处理传过来的字符串转化为数组
let dataArr = date.split(" ")[0].split('/')
let time = date.split(" ")[1].split(':')
// 默认开始显示数据 如果把自定义值传过来的话就使用自定义时间 否则用当前时间 ...dataArr数组解构
var defaultDate = date ? [...dataArr, ...time] : getNewDateArry();
// 处理联动列表数据
/*年月日 时分秒*/
dateTimeArray[0] = getLoopArray(start,end);
dateTimeArray[1] = getLoopArray(1, 12);
dateTimeArray[2] = getMonthDay(defaultDate[0], defaultDate[1]);
dateTimeArray[3] = getLoopArray(0, 23);
dateTimeArray[4] = getLoopArray(0, 59);
// dateTimeArray[5] = getLoopArray(0, 59);
//遍历dateTimeArray数组
dateTimeArray.forEach((current,index) =>
//匹配defaultDate[index]值在current数组中的位置
dateTime.push(current.indexOf(defaultDate[index]))
);
return {
dateTimeArray: dateTimeArray,
dateTime: dateTime
}
}
module.exports = {
dateTimePicker: dateTimePicker,
getMonthDay: getMonthDay
}
template模板
<picker mode="multiSelector" :range="dateTimeArray" v-model="dateTime" @change="change" @columnchange="columnchange">
<view>
//disable = "true" 是禁用input 防止点击弹出键盘
<input type="text" disabled="true" v-model="upTower" placeholder="请选择时间" placeholder-class="inputPlace" />
</view>
</picker>
script部分
//引入事先声明的类
const dateTimePicker = require('@/util/dateTimePicker.js')
export default {
data() {
return {
// 时间选择器
dateTimeArray: null,
dateTime: null,
startYear: 2000,
endYear: 2050,
};
},
onLoad() {
//传入开始展示年份startYear 结束展示年份endYear
//自定义开始显示时间
let arr = '2018/09/01 13:00'
let obj = dateTimePicker.dateTimePicker(this.startYear, this.endYear,arr)
this.dateTimeArray = obj.dateTimeArray
this.dateTime = obj.dateTime
},
methods: {
//时间格式化
withData(param){
return param < 10 ? '0' + param : '' + param;
},
change(e){
let value = []
e.detail.value.forEach((val,index) => {
value.push(this.withData(val))
})
let dateArray = "20" + value[0] + "-" + value[1] + "-" + value[2] + " " + value[3] + ":" + value[4]
this.upTower = dateArray
},
columnchange(e){
let dateArr = this.dateTimeArray
let arr = this.dateTime
//滑动所在列的数据并对其值进行更新
arr[e.detail.column] = e.detail.value
//更新展示月份对应的天数(28 or 29 or 30 or 31)
dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]])
//最后把最新的数值赋值到dateTimeArray
this.dateTimeArray = dateArr
this.dateTime = arr
},
picker属性中 :range=“dateTimeArray” =》dateTimeArray 二维数组,长度表示多少列,数组的每项表示每列的数据 这是多列选择器展示的重点所在