<picker
mode=”multiSelector”
class=”form-control”
:value=”dateTimes”
@change=”changTime”
@columnchange=”changeDateTimeColumn1″
:range=”dateTimeArray1″
>
<view class=”btn-choose”>
{{ form.expected_time && form.expected_time!=” && form.expected_time!=’undefined’ ? form.expected_time : ‘请选择’ }}
<image class=”ico ico-arrow-r” src=”@/static/arrow_r.png”></image>
</view>
</picker>
<script>
import dateTimePicker from ‘@/utils/dateTimePicker’;
export default {
onLoad(options) {
// 获取完整的年月日 时分秒,以及默认显示的数组
var arrs = dateTimePicker.dateTimePicker(yearn, yearn + 1);
//console.log(arrs);
/*
dateTime: (5) [0, 8, 19, 14, 36]
dateTimeArray: Array (5)
[“2024”, “2025”],
[“01”, “02”, “03”, “04”, “05”, “06”, “07”, “08”, “09”, “10”, “11”, “12”],
[“01”, “02”, “03”, “04”, “05”, “06”, “07”, “08”, “09”, …, “29”, “30”],
[“00”, “01”, “02”, “03”, “04”, “05”, “06”, “07”, “08”, …, “22”, “23”],
[“01”, “02”, “03”, “04”, “05”, “06”, “07”, “08”, “09”, …, “58”, “59”],
*/
// 精确到分的处理,将数组的秒去掉
var lastArray = arrs.dateTimeArray.pop();
var lastTime = arrs.dateTime.pop();
this.setData({
dateTimeArray: arrs.dateTimeArray,
dateTimes: arrs.dateTime
});
},
methods: {
//时间选择器
changTime(e) {
let val = e.detail.value;
let drr = this.dateTimeArray;
this.form.expected_time = drr[0][dateTime1[0]] +’-‘ +drr[1][val[1]] +’-‘ +drr[2][val[2]] +’ ‘ +drr[3][val[3]] +’:’ +drr[4][val[4]];
this.setData({
dateTimes: e.detail.value,
form:this.form
});
this.$forceUpdate();
},
//时间选择器第一行
changeDateTimeColumn1(e) {
var arr = this.dateTimes;
var drr = this.dateTimeArray;
arr[e.detail.column] = e.detail.value;
//重新计算对应月份的日期列表
drr[2] = dateTimePicker.getMonthDay(drr[0][arr[0]], drr[1][arr[1]]);
console.log(‘dateArr’, drr)
this.setData({
dateTimeArray: drr,
dateTimes: arr
});
this.$forceUpdate();
},
}
}
</script>
utils/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 }; } /** * 获取本周、本季度、本月、上月的开端日期、停止日期 */ //当前日期 var now = new Date(); //今天本周的第几天 var nowDayOfWeek = now.getDay(); //当前日 var nowDay = now.getDate(); //当前月 var nowMonth = now.getMonth(); //当前年 var nowYear = now.getYear(); nowYear += nowYear < 2000 ? 1900 : 0; //上月日期 var lastMonthDate = new Date(); lastMonthDate.setDate(1); lastMonthDate.setMonth(lastMonthDate.getMonth() - 1); var lastYear = lastMonthDate.getYear(); var lastMonth = lastMonthDate.getMonth(); //格局化日期:yyyy-MM-dd function formatDate(date) { var myyear = date.getFullYear(); var mymonth = date.getMonth() + 1; var myweekday = date.getDate(); if (mymonth < 10) { mymonth = '0' + mymonth; } if (myweekday < 10) { myweekday = '0' + myweekday; } return myyear + '-' + mymonth + '-' + myweekday; } //获得某月的天数 function getMonthDays(myMonth) { var monthStartDate = new Date(nowYear, myMonth, 1); var monthEndDate = new Date(nowYear, myMonth + 1, 1); var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24); return days; } //获得本周的开端日期 function getWeekStartDate() { var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek); return formatDate(weekStartDate); } //获得本周的停止日期 function getWeekEndDate() { var weekEndDate = new Date(nowYear, nowMonth, nowDay + (7 - nowDayOfWeek)); return formatDate(weekEndDate); } //获得本月的开端日期 function getMonthStartDate() { var monthStartDate = new Date(nowYear, nowMonth, 1); return formatDate(monthStartDate); } //获得本月的停止日期 function getMonthEndDate() { var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth)); return formatDate(monthEndDate); } //获得上月开端时候 function getLastMonthStartDate() { var lastMonthStartDate = new Date(nowYear, lastMonth, 1); return formatDate(lastMonthStartDate); } //获得上月停止时候 function getLastMonthEndDate() { var lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(lastMonth)); return formatDate(lastMonthEndDate); } //获得本季度的开端月份 function getQuarterStartMonth() { var quarterStartMonth = 0; if (nowMonth < 3) { quarterStartMonth = 0; } if (2 < nowMonth && nowMonth < 6) { quarterStartMonth = 3; } if (5 < nowMonth && nowMonth < 9) { quarterStartMonth = 6; } if (nowMonth > 8) { quarterStartMonth = 9; } return quarterStartMonth; } //获得本季度的开端日期 function getQuarterStartDate() { var quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1); return formatDate(quarterStartDate); } //获得本季度的停止日期 function getQuarterEndDate() { var quarterEndMonth = getQuarterStartMonth() + 2; var quarterStartDate = new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth)); return formatDate(quarterStartDate); } module.exports = { dateTimePicker: dateTimePicker, getMonthDay: getMonthDay, //获得本周的开端日期 getWeekStartDate: getWeekStartDate, //获得本周的停止日期 getWeekEndDate: getWeekEndDate, //获得本月的开端日期 getMonthStartDate: getMonthStartDate, //获得本月的停止日期 getMonthEndDate: getMonthEndDate, //获得上月开端时候 getLastMonthStartDate: getLastMonthStartDate, //获得上月停止时候 getLastMonthEndDate: getLastMonthEndDate };
111