微信小程序日期时间选择器使用方法
程序员文章站
2022-05-15 08:09:45
本文实例为大家分享了精确到秒的微信小程序日期时间选择器,供大家参考,具体内容如下
效果图
实现原理
利用微信小程序的picker组件的多列选择器实现!
wxml...
本文实例为大家分享了精确到秒的微信小程序日期时间选择器,供大家参考,具体内容如下
效果图
实现原理
利用微信小程序的picker组件的多列选择器实现!
wxml
<view class="tui-picker-content"> <view class="tui-picker-name">时间选择器(选择时分)</view> <picker mode="time" value="{{time}}" start="09:00" end="17:30" bindchange="changetime"> <view class="tui-picker-detail"> 午饭时间: {{time}} </view> </picker> </view> <view class="tui-picker-content"> <view class="tui-picker-name">日期选择器(选择年月日)</view> <picker mode="date" value="{{date}}" start="2017-10-01" end="2017-10-08" bindchange="changedate"> <view class="tui-picker-detail"> 国庆出游: {{date}} </view> </picker> </view> <view class="tui-picker-content"> <view class="tui-picker-name">日期时间选择器(精确到秒)</view> <picker mode="multiselector" value="{{datetime}}" bindchange="changedatetime" bindcolumnchange="changedatetimecolumn" range="{{datetimearray}}"> <view class="tui-picker-detail"> 选择日期时间: {{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 class="tui-picker-content"> <view class="tui-picker-name">日期时间选择器(精确到分)</view> <picker mode="multiselector" value="{{datetime1}}" bindchange="changedatetime1" bindcolumnchange="changedatetimecolumn1" range="{{datetimearray1}}"> <view class="tui-picker-detail"> 选择日期时间: {{datetimearray1[0][datetime1[0]]}}-{{datetimearray1[1][datetime1[1]]}}-{{datetimearray1[2][datetime1[2]]}} {{datetimearray1[3][datetime1[3]]}}:{{datetimearray1[4][datetime1[4]]}} </view> </picker> </view>
wxss
@import "../picker/picker.wxss";
使用的是三级联动选择器的样式,所以直接 import 引入!
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(){ // 获取完整的年月日 时分秒,以及默认显示的数组 var obj = datetimepicker.datetimepicker(this.data.startyear, this.data.endyear); var obj1 = datetimepicker.datetimepicker(this.data.startyear, this.data.endyear); // 精确到分的处理,将数组的秒去掉 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 = '月份格式不正确,请重新输入!' } 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 }
总结
- 将初始化列表以及初始化默认显示的数组放到datetimepicker.js,防止页面逻辑太乱,而且可以多处使用;
- 判断是否为闰年,在三木表达式中,必须将能别400整除放在前边,因为或运算只要一个条件满足,就会返回true,不会执行后续表达式;
- switch case的合并方法需要注意格式;
- 如果只需要联动列表更新,二结果展示栏不更新,则在changedatetimecolumn函数中只更新datetimearray的值。
demo下载
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。