1.实现效果
2.实现原理
mode='multiSelector'多列选择器
自定义范围数组range
3.实现代码文章来源:https://www.toymoban.com/news/detail-571843.html
<!--pages/jsCase/multiTime/index4.wxml-->
<view class="act_box">
<view class="act_item flex j_b">
<view class="nowp"><text class="require">*</text>
开始时间:
</view>
<picker mode="multiSelector" value="{{start_time}}" data-type="start_time" data-param='start_time_p' bindchange="changeDateTime" bindcolumnchange="changeDateTimeColumn" range="{{dateTimeArray}}">
<view class="flex-row act_right">
<text wx:if="{{!start_time_p}}">请选择</text>
<text wx:else class="sel_text">{{start_time_p}}</text>
</view>
</picker>
</view>
<view class="act_item flex j_b">
<view class="nowp"><text class="require">*</text>
结束时间:
</view>
<picker mode="multiSelector" value="{{end_time}}" data-type="end_time" data-param='end_time_p' bindchange="changeDateTime" bindcolumnchange="changeDateTimeColumn" range="{{dateTimeArray}}">
<view class="flex-row act_right">
<text wx:if="{{!end_time_p}}">请选择</text>
<text wx:else class="sel_text">{{end_time_p}}</text>
</view>
</picker>
</view>
</view>
/* pages/jsCase/multiTime/index4.wxss */
.act_item {
width: 702rpx;
background: #FFFFFF;
border-radius: 16rpx;
margin-bottom: 24rpx;
box-sizing: border-box;
padding: 24rpx;
position: relative;
font-size: 28rpx;
color: #4D4D4D;
font-weight: 500;
}
.require {
color: #E9543E;
}
.nowp {
white-space: nowrap;
margin-right: 15rpx;
}
.act_right {
display: flex;
justify-content: flex-end;
font-size: 28rpx;
color: #999;
}
.act_item picker {
flex: 1;
margin-left: 40rpx;
}
.act_box {
margin: 24rpx auto;
width: 710rpx;
}
.sel_text {
font-size: 28rpx;
font-weight: 500;
color: #4D4D4D;
}
// pages/jsCase/multiTime/index4.js
var dateTimePicker = require('../util/dateTimer.js');
Page({
data: {
end_time: '',
start_time: '',
dateTimeArray: '', //时间数组
startYear: 2000, //最小年份
endYear: 2050, // 最大年份
start_time_p: '', //显示的开始时间
start_time_p: '', //显示的结束时间
},
onLoad: function (options) {
// 获取完整的年月日 时分秒,以及默认显示的数组
var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
this.setData({
start_time: obj.dateTime,
end_time: obj.dateTime,
dateTimeArray: obj.dateTimeArray,
});
},
/**
* 选择时间
* @param {*} e
*/
changeDateTime(e) {
let dateTimeArray = this.data.dateTimeArray,
{
type,
param
} = e.currentTarget.dataset;
this.setData({
[type]: e.detail.value,
[param]: dateTimeArray[0][e.detail.value[0]] + '-' + dateTimeArray[1][e.detail.value[1]] + '-' + dateTimeArray[2][e.detail.value[2]] + ' ' + dateTimeArray[3][e.detail.value[3]] + ':' + dateTimeArray[4][e.detail.value[4]] + ':' + dateTimeArray[5][e.detail.value[5]]
});
},
changeDateTimeColumn(e) {
var dateArr = this.data.dateTimeArray,
{
type
} = e.currentTarget.dataset,
arr = this.data[type];
arr[e.detail.column] = e.detail.value;
dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
this.setData({
dateTimeArray: dateArr,
[type]: arr
});
},
})
自定义range数组:文章来源地址https://www.toymoban.com/news/detail-571843.html
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, seco];
}
function dateTimePicker(startYear, endYear, date) {
// 返回默认显示的数组和联动数组的声明
var dateTime = [],
dateTimeArray = [
[],
[],
[],
[],
[],
[]
];
var start = startYear || 1978;
var end = endYear || 2100;
// 默认开始显示数据
var defaultDate = date ? [...date.split(' ')[0].split('-'), ...date.split(' ')[1].split(':')] : 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.forEach((current, index) => {
dateTime.push(current.indexOf(defaultDate[index]));
});
return {
dateTimeArray: dateTimeArray,
dateTime: dateTime
}
}
module.exports = {
dateTimePicker: dateTimePicker,
getMonthDay: getMonthDay
}
到了这里,关于微信小程序多列选择器实现日期+时间的选择的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!