小程序时间选择器精确到年月日时分秒
html:
<view class="tui-picker-content"> <picker mode="multiSelector" value="{{startTime}}" bindchange="changeStartDateTime" bindcolumnchange="changeStartTimeColumn" range="{{startTimeArray}}"> <view class="tui-picker-detail"> 开始时间: {{startTimeArray[0][startTime[0]]}} -{{startTimeArray[1][startTime[1]]}} -{{startTimeArray[2][startTime[2]]}} {{startTimeArray[3][startTime[3]]}} :{{startTimeArray[4][startTime[4]]}} :{{startTimeArray[5][startTime[5]]}} </view> </picker> </view> <view class="tui-picker-content"> <picker mode="multiSelector" value="{{endTime}}" bindchange="changeEndDateTime" bindcolumnchange="changeEndTimeColumn" range="{{endTimeArray}}"> <view class="tui-picker-detail"> 结束时间: {{endTimeArray[0][endTime[0]]}} -{{endTimeArray[1][endTime[1]]}} -{{endTimeArray[2][endTime[2]]}} {{endTimeArray[3][endTime[3]]}} :{{endTimeArray[4][endTime[4]]}} :{{endTimeArray[5][endTime[5]]}} </view> </picker> </view>
js:
// pages/changePassword/changePassword.js var app = getApp() var dateTimePicker = require('../../utils/dateTimePicker'); Page({ /** * 页面的初始数据 */ data: { date: '2018-10-01', time: '12:00', startTimeArray: null, endTimeArray: null, startTime: null, endTime: null, dateTimeArray1: null, dateTime1: null, startYear: 2000, endYear: 2050 }, changeDate(e) { console.log("changeDate............"); this.setData({ date: e.detail.value }); }, changeTime(e) { console.log("changeTime............"); this.setData({ time: e.detail.value }); }, changeStartDateTime(e) { const that = this; console.log("打印时间~~~~~~~~~~~~~~~~~~~~~", this.data.startTime); this.setData({ startTime: e.detail.value }); console.log("打印时间", this.data.startTime); var aaa1 = that.data.startTime[0]; var aaa2 = that.data.startTime[1]; var aaa3 = that.data.startTime[2]; var aaa4 = that.data.startTime[3]; var aaa5 = that.data.startTime[4]; var aaa6 = that.data.startTime[5]; var time1 = that.data.startTimeArray[0][aaa1]; var time2 = that.data.startTimeArray[1][aaa2]; var time3 = that.data.startTimeArray[2][aaa3]; var time4 = that.data.startTimeArray[3][aaa4]; var time5 = that.data.startTimeArray[4][aaa5]; var time6 = that.data.startTimeArray[5][aaa6]; var time = time1 + '-' + time2 + '-' + time3 + ' ' + time4 + ':' + time5 + ':' + time6; console.log("开始时间:", time); }, changeEndDateTime(e) { const that = this; // console.log("打印时间~~~~~~~~~~~~~~~~~~~~~", this.data.endTimeArray); this.setData({ endTime: e.detail.value }); // console.log("打印时间", this.data.endTime); var aaa1 = that.data.endTime[0]; var aaa2 = that.data.endTime[1]; var aaa3 = that.data.endTime[2]; var aaa4 = that.data.endTime[3]; var aaa5 = that.data.endTime[4]; var aaa6 = that.data.endTime[5]; var time1 = that.data.endTimeArray[0][aaa1]; var time2 = that.data.endTimeArray[1][aaa2]; var time3 = that.data.endTimeArray[2][aaa3]; var time4 = that.data.endTimeArray[3][aaa4]; var time5 = that.data.endTimeArray[4][aaa5]; var time6 = that.data.endTimeArray[5][aaa6]; var time = time1 + '-' + time2 + '-' + time3 + ' ' + time4 + ':' + time5 + ':' + time6; console.log("结束时间:", time); }, changeDateTime1(e) { console.log("changeDateTime1............"); this.setData({ dateTime1: e.detail.value }); }, changeStartTimeColumn(e) { var arr = this.data.startTime, dateArr = this.data.startTimeArray; arr[e.detail.column] = e.detail.value; dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]); this.setData({ startTimeArray: dateArr, startTime: arr }); }, changeEndTimeColumn(e) { var arr = this.data.endTime, dateArr = this.data.endTimeArray; arr[e.detail.column] = e.detail.value; dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]); this.setData({ endTimeArray: dateArr, endTime: arr }); }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { // 获取完整的年月日 时分秒,以及默认显示的数组 var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear); var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear); // console.log("obj"); // console.log(obj); // console.log("obj1"); // console.log(obj1); // 精确到分的处理,将数组的秒去掉 var lastArray = obj1.dateTimeArray.pop(); var lastTime = obj1.dateTime.pop(); this.setData({ startTime: obj.dateTime, startTimeArray: obj.dateTimeArray, endTimeArray: obj.dateTimeArray, endTime: obj.dateTime }); // console.log("startTimeArray"); // console.log(this.data.startTimeArray); // console.log("endTimeArray"); // console.log(this.data.startTimeArray); }, })
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, 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 }