本文
南京小程序开发主要介绍如何在微信小程序中实现精确的日期 时间 选择器。本文将详细介绍示例代码,它对每个人的学习或工作都有一定的参考学习价值。您可以为您的朋友参考以下内容
声明
错误:由于此博客正在处理bindcolumnchange事件中的值更改,因此似乎表明,当您选择时,您没有单击“确定”,而是直接取消返回,您会发现选择框的值仍在更改。
原因:这是由bindcolumnchange事件中的值更改处理引起的。
处理方法:如果需要在确认后更改该值,请将bindcolumnchange事件中的处理操作放入bindchange事件中。
设计草图
实现原理
使用微信小程序选择器组件的多列选择器!
XML文件
<view>
< View > time selector (select time) < View >
<picker mode="time" value="{{time}}" start="09:00" end="17:30" bindchange="changeTime">
<view>
Lunch time: {time}}
</view>
</picker>
</view>
<view>
< View > date selector (select mm / DD / yyyy) < View >
<picker mode="date" value="{{date}}" start="2017-10-01" end="2017-10-08" bindchange="changeDate">
<view>
National Day travel: {{date}}
</view>
</picker>
</view>
<view>
< View > date time selector (accurate to seconds) < / View >
<picker mode="multiSelector" value="{{dateTime}}" bindchange="changeDateTime" bindcolumnchange="changeDateTimeColumn" range="{{dateTimeArray}}">
<view>
Select date time: {datetimearray [0] [datetime [0]]}} - {datetimearray [1] [datetime [1]]}} - {datetimearray [2] [datetime [2]]}} {datetimearray [3] [datetime [3]]}}: {datetimearray [4] [datetime [4]]}}: {datetimearray [5] [datetime [5]]}}}
</view>
</picker>
</view>
<view>
< View > date time selector (accurate to minute) < / View >
<picker mode="multiSelector" value="{{dateTime1}}" bindchange="changeDateTime1" bindcolumnchange="changeDateTimeColumn1" range="{{dateTimeArray1}}">
<view>
Select date time: {datetimearray1 [0] [datetime1 [0]]}} - {datetimearray1 [1] [datetime1 [1]]}} - {datetimearray1 [2] [datetime1 [2]]} {{datetimearray1 [3] [datetime1 [3]]}}: {{datetimearray1 [4] [datetime1 [4]]}}}
</view>
</picker>
</view>
JS
var dateTimePicker = require('../../utils/dateTimePicker.js');
Page({
data: {
date: '2018-10-01',
time: '12:00',
dateTimeArray: null,
dateTime: null,
dateTimeArray1: null,
dateTime1: null,
startYear: 2000,
endYear: 2050
},
onLoad(){
//Get the complete month, day, hour, minute, second, and the array displayed by default
var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
//Processing accurate to minutes, removing the seconds of the array
var lastArray = obj1.dateTimeArray.pop();
var lastTime = obj1.dateTime.pop();
this.setData({
dateTime: obj.dateTime,
dateTimeArray: obj.dateTimeArray,
dateTimeArray1: obj1.dateTimeArray,
dateTime1: obj1.dateTime
});
},
changeDate(e){
this.setData({ date:e.detail.value});
},
changeTime(e){
this.setData({ time: e.detail.value });
},
changeDateTime(e){
this.setData({ dateTime: e.detail.value });
},
changeDateTime1(e) {
this.setData({ dateTime1: e.detail.value });
},
changeDateTimeColumn(e){
var arr = this.data.dateTime, dateArr = this.data.dateTimeArray;
arr[e.detail.column] = e.detail.value;
dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
this.setData({
dateTimeArray: dateArr,
dateTime: arr
});
},
changeDateTimeColumn1(e) {
var arr = this.data.dateTime1, dateArr = this.data.dateTimeArray1;
arr[e.detail.column] = e.detail.value;
dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
this.setData({
dateTimeArray1: dateArr,
dateTime1: arr
});
}
})
外部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 = 'month format is incorrect, please re-enter! '
}
return array;
}
function getNewDateArry(){
//Processing of current time
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) {
//Returns the declaration of default display array and linkage array
var dateTime = [], dateTimeArray = [[],[],[],[],[],[]];
var start = startYear || 1978;
var end = endYear || 2100;
//Default start data display
var defaultDate = date ? [...date.split(' ')[0].split('-'), ...date.split(' ')[1].split(':')] : getNewDateArry();
//Processing linkage list data
/*Mm / DD / yyyy HHM / S*/
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
}
摘要
1.将初始化列表和初始化默认显示数组放入datetimepicker.js中,以防止页面逻辑过于混乱并可以在多个地方使用;
2.要确定是否为a年,必须在三个木表达式中将400的除数放在前面,因为只要满足一个条件,否则运算将返回true,并且不执行任何后续表达式;
3.开关盒的合并方法需要注意格式;
4.如果您只需要更新链接列表,并且结果显示栏没有更新,则changedatetimecolumn函数中只会更新datetimearray的值。
以上是南京小程序开发本文的全部内容。希望对您的学习有所帮助,也希望您能提供更多支持。