背景
由于官方只提供了单独的日期选择和时间选择,在项目中遇到需要同时选择日期和时间的需求,所以自己写了一个可以动态配置格式的时间选择器。
效果图
首先我们新建自定义组件,不明白的可以看官方文档 https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/
新建自定义组件
界面编写
datetimepicker.wxml
<view class="view-body">
<text class='item-key'>{{title}}<text style="color:red" wx:if="{{isRequired}}">*</text></text>
<view class="item-value">
<picker mode="multiSelector" bindchange="bindPickerChange" bindcolumnchange="bindPickerColumnChange" value="{{multiIndex}}" range="{{multiArray}}">
<input disabled="{{true}}" value='{{value}}' name='{{name}}' />
<image class="img-arrow" src='/images/date_icon.png' />
</picker>
</view>
</view>
js代码
// components/datatimepicker/datatimepicker.js
Component({
behaviors: ['wx://form-field'],
properties: {
title: {
type: String
},
name: {
type: String
},
isRequired: {
type: Boolean
},
format: {
type: String
}
},
data: {},
lifetimes: {
attached: function() {
//当前时间 年月日 时分秒
const date = new Date()
const curYear = date.getFullYear()
const curMonth = date.getMonth() + 1
const curDay = date.getDate()
const curHour = date.getHours()
const curMinute = date.getMinutes()
const curSecond = date.getSeconds()
//初始化时间选择轴
this.setData({
chooseYear: curYear
})
this.initColumn(curMonth)
//不足两位的前面好补0 因为后面要获取在时间轴上的索引 时间轴初始化的时候都是两位
var showMonth = curMonth < 10 ? ('0' + curMonth) : curMonth
var showDay = curDay < 10 ? ('0' + curDay) : curDay
var showHour = curHour < 10 ? ('0' + curHour) : curHour
var showMinute = curMinute < 10 ? ('0' + curMinute) : curMinute
var showSecond = curSecond < 10 ? ('0' + curSecond) : curSecond
//当前时间在picker列上面的索引 为了当打开时间选择轴时选中当前的时间
var indexYear = this.data.years.indexOf(curYear + '')
var indexMonth = this.data.months.indexOf(showMonth + '')
var indexDay = this.data.days.indexOf(showDay + '')
var indexHour = this.data.hours.indexOf(showHour + '')
var indexMinute = this.data.minutes.indexOf(showMinute + '')
var indexSecond = this.data.seconds.indexOf(showSecond + '')
var multiIndex = []
var multiArray = []
var value = ''
var format = this.properties.format;
if (format == 'yyyy-MM-dd') {
multiIndex = [indexYear, indexMonth, indexDay]
value = `${curYear}-${showMonth}-${showDay}`
multiArray = [this.data.years, this.data.months, this.data.days]
}
if (format == 'HH:mm:ss') {
multiIndex = [indexHour, indexMinute, indexSecond]
value = `${showHour}:${showMinute}:${showSecond}`
multiArray = [this.data.hours, this.data.minutes, this.data.seconds]
}
if (format == 'yyyy-MM-dd HH:mm') {
multiIndex = [indexYear, indexMonth, indexDay, indexHour, indexMinute]
value = `${curYear}-${showMonth}-${showDay} ${showHour}:${showMinute}`
multiArray = [this.data.years, this.data.months, this.data.days, this.data.hours, this.data.minutes]
}
if (format == 'yyyy-MM-dd HH:mm:ss') {
multiIndex = [indexYear, indexMonth, indexDay, indexHour, indexMinute, indexSecond]
value = `${curYear}-${showMonth}-${showDay} ${showHour}:${showMinute}:${showSecond}`
multiArray = [this.data.years, this.data.months, this.data.days, this.data.hours, this.data.minutes, this.data.seconds]
}
this.setData({
value,
multiIndex,
multiArray,
curMonth,
chooseYear: curYear,
})
}
},
/**
* 组件的方法列表
*/
methods: {
//获取时间日期
bindPickerChange: function(e) {
this.setData({
multiIndex: e.detail.value
})
const index = this.data.multiIndex
var format = this.properties.format
var showTime = ''
if (format == 'yyyy-MM-dd') {
const year = this.data.multiArray[0][index[0]]
const month = this.data.multiArray[1][index[1]]
const day = this.data.multiArray[2][index[2]]
showTime = `${year}-${month}-${day}`
}
if (format == 'HH:mm:ss') {
const hour = this.data.multiArray[0][index[0]]
const minute = this.data.multiArray[1][index[1]]
const second = this.data.multiArray[2][index[2]]
showTime = `${hour}:${minute}:${second}`
}
if (format == 'yyyy-MM-dd HH:mm') {
const year = this.data.multiArray[0][index[0]]
const month = this.data.multiArray[1][index[1]]
const day = this.data.multiArray[2][index[2]]
const hour = this.data.multiArray[3][index[3]]
const minute = this.data.multiArray[4][index[4]]
showTime = `${year}-${month}-${day} ${hour}:${minute}`
}
if (format == 'yyyy-MM-dd HH:mm:ss') {
const year = this.data.multiArray[0][index[0]]
const month = this.data.multiArray[1][index[1]]
const day = this.data.multiArray[2][index[2]]
const hour = this.data.multiArray[3][index[3]]
const minute = this.data.multiArray[4][index[4]]
const second = this.data.multiArray[5][index[5]]
showTime = `${year}-${month}-${day} ${hour}:${minute}:${second}`
}
this.setData({
value: showTime
})
this.triggerEvent('dateTimePicker', showTime)
},
//初始化时间选择轴
initColumn(curMonth) {
const years = []
const months = []
let days = []
const hours = []
const minutes = []
const seconds = []
for (let i = 1990; i <= 2099; i++) {
years.push(i + '')
}
for (let i = 1; i <= 12; i++) {
if (i < 10) {
i = "0" + i;
}
months.push(i + '')
}
if (curMonth == 1 || curMonth == 3 || curMonth == 5 || curMonth == 7 || curMonth == 8 || curMonth == 10 || curMonth == 12) {
for (let i = 1; i <= 31; i++) {
if (i < 10) {
i = "0" + i;
}
days.push(i + '')
}
}
if (curMonth == 4 || curMonth == 6 || curMonth == 9 || curMonth == 11) {
for (let i = 1; i <= 30; i++) {
if (i < 10) {
i = "0" + i;
}
days.push(i + '')
}
}
if (curMonth == 2) {
this.setFebDays()
}
for (let i = 0; i <= 23; i++) {
if (i < 10) {
i = "0" + i;
}
hours.push(i + '')
}
for (let i = 0; i <= 59; i++) {
if (i < 10) {
i = "0" + i;
}
minutes.push(i + '')
}
for (let i = 0; i <= 59; i++) {
if (i < 10) {
i = "0" + i;
}
seconds.push(i + '')
}
this.setData({
years,
months,
days,
hours,
minutes,
seconds
})
},
/**
* 列改变时触发
*/
bindPickerColumnChange: function(e) {
//获取年份 用于计算改年的2月份为平年还是闰年
if (e.detail.column == 0 && this.properties.format != 'HH:mm:ss') {
var chooseYear = this.data.multiArray[e.detail.column][e.detail.value];
this.setData({
chooseYear
})
if (this.data.curMonth == '02' || this.data.chooseMonth == '02') {
this.setFebDays()
}
}
//当前第二为月份时需要初始化当月的天数
if (e.detail.column == 1 && this.properties.format != 'HH:mm:ss') {
let num = parseInt(this.data.multiArray[e.detail.column][e.detail.value]);
let temp = [];
if (num == 1 || num == 3 || num == 5 || num == 7 || num == 8 || num == 10 || num == 12) { //31天的月份
for (let i = 1; i <= 31; i++) {
if (i < 10) {
i = "0" + i;
}
temp.push("" + i);
}
this.setData({
['multiArray[2]']: temp
});
} else if (num == 4 || num == 6 || num == 9 || num == 11) { //30天的月份
for (let i = 1; i <= 30; i++) {
if (i < 10) {
i = "0" + i;
}
temp.push("" + i);
}
this.setData({
['multiArray[2]']: temp
});
} else if (num == 2) { //2月份天数
this.setFebDays()
}
}
var data = {
multiArray: this.data.multiArray,
multiIndex: this.data.multiIndex
};
data.multiIndex[e.detail.column] = e.detail.value;
this.setData(data);
},
//计算二月份天数
setFebDays() {
let year = parseInt(this.data.chooseYear);
let temp = [];
if (year % (year % 100 ? 4 : 400) ? false : true) {
for (let i = 1; i <= 29; i++) {
if (i < 10) {
i = "0" + i;
}
temp.push("" + i);
}
this.setData({
['multiArray[2]']: temp,
chooseMonth: '02'
});
} else {
for (let i = 1; i <= 28; i++) {
if (i < 10) {
i = "0" + i;
}
temp.push("" + i);
}
this.setData({
['multiArray[2]']: temp,
chooseMonth: '02'
});
}
retrun temp;
}
},
})
使用方法:
首先在引用page的json中引用自定义组件:名称(DateTimePicker)为wxml中加载的标签名 可以随意名称 两边保持一致即可,路径确保正确。
{
"usingComponents": {
"DateTimePicker": "/components/datetimepicker/datetimepicker"
}
}
wxml中:
<DateTimePicker title="选择时间" isRequired="true" bind:dateTimePicker="onDateTimePicker" name='time' format='yyyy-MM-dd HH:mm:ss' />
title:表单组件的名称
isRequired:是否必填项
dateTimePicker为选中确认回调
name为表单点击 form 表单中 form-type 为 submit 的 button 组件时,会将表单组件中的 value 值进行提交,需要在表单组件中加上 name 来作为 key
format:时间格式化参数 支持:'yyyy-MM-dd HH:mm:ss','yyyy-MM-dd HH:mm','yyyy-MM-dd','HH-mm-ss' 四种
最后附上完整代码地址:https://github.com/tex-github/miniprogram-components-datetimepicker