赞
踩
先看效果:
示例.vue
<template> <!-- 设置时间 --> <view class="pick-Outer"> <view class="pick-view Underline"> <view> <text>设置开始时间</text> <picker class="flex-left" mode="multiSelector" :range="Time.start_arr" :value="Time.multiIndex_a" range-key="name" @columnchange="colStart" @change="changeStart"> <view> <text class="pick-time">{{Time.start}}</text> <image src="/static/detail/xiangyou-jiantou.svg" mode="widthFix"></image> </view> </picker> </view> </view> <!-- 结束时间 --> <view class="pick-view"> <view> <text>设置结束时间</text> <picker class="flex-left" mode="multiSelector" :range="Time.end_arr" :value="Time.multiIndex_b" range-key="name" @columnchange="colEnd" @change="changeEnd"> <view> <text class="pick-time">{{Time.end}}</text> <image src="/static/detail/xiangyou-jiantou.svg" mode="widthFix"></image> </view> </picker> </view> </view> </view> </template> <script setup> import { ref, onMounted, reactive, toRefs, watch, toRaw } from 'vue' import { start_date, end_date } from '@/Acc-config/date.js' import { current, months, codays } from '@/Acc-config/ca-time.js' current() const Time = reactive({ start_arr: start_date, end_arr: end_date, multiIndex_a: [0, 0, 0, 0, 0], //开始时间value 每一项的值 multiIndex_b: [0, 0, 0, 0, 0], //结束时间value 每一项的值 start: '', //开始时间 end: '', //结束时间 }) // 开始时间和结束时间滚动时触发公用方法:从新计算某年某月的天数 function shAre(RES, to_date, mult, val) { console.log('修改的列为:' + RES.column + ',值为:' + RES.value) mult[RES.column] = RES.value switch (RES.column) { case 0: //拖动第1列:年 switch (mult[0]) { case 0: //第一列的第一个值:当前年 to_date[1] = months(to_date[0][0].time) to_date[2] = codays({ year: to_date[0][0].time, month: to_date[1][0].time }) break; case 1: //第一列的第二个值:明年 to_date[1] = months(to_date[0][1].time) to_date[2] = codays({ year: to_date[0][1].time, month: -1 }) break; } mult.splice(1, 1, 0) mult.splice(2, 1, 0) mult.splice(3, 1, 0) mult.splice(4, 1, 0) break; case 1: //拖动第二列:月 let MO = mult to_date[2] = codays({ year: to_date[0][MO[0]].time, month: to_date[1][MO[1]].time }) mult.splice(2, 1, 0) mult.splice(3, 1, 0) mult.splice(4, 1, 0) break; } } // 开始时间:滚动时触发 function colStart(event) { const RES = event.detail shAre(RES, Time.start_arr, Time.multiIndex_a, 'start') } // 结束时间:滚动时触发 function colEnd(event) { const RES = event.detail shAre(RES, Time.end_arr, Time.multiIndex_b, 'end') } // 开始时间:确定 function changeStart(e) { const RES = e.detail.value conFirm(RES, 'start', Time.start_arr) } // 结束时间:确定 function changeEnd(e) { const RES = e.detail.value conFirm(RES, 'end', Time.end_arr) } // 开始时间和结束时间:确定=》公用 function conFirm(RES, val, date) { const year = date[0][RES[0]].time const month = date[1][RES[1]].time const day = date[2][RES[2]].time const time = date[3][RES[3]].time const minute = date[4][RES[4]].time const sele_res = year + '/' + month + '/' + day + ' ' + time + ':' + minute + ':' + '00' if (val == 'start') { // 开始时间 Time.start = sele_res Time.multiIndex_a = RES } else { // 结束时间 Time.end = sele_res Time.multiIndex_b = RES } } // 监听结束时间是否小于开始时间 import moment from 'moment' moment.locale('zh-cn'); watch([() => Time.start, () => Time.end], (newVal, oldVal) => { if (newVal[0] != '' && newVal[1] != '') { const start = moment(newVal[0], 'YYYY/MM/DD hh:mm:ss').unix() //开始时间 const end = moment(newVal[1], 'YYYY/MM/DD hh:mm:ss').unix() //结束时间 if (start >= end) { Time.end = '结束时间早已开始时间' } } }) </script> <style scoped> .pick-Outer { background-color: #f7f7f7; margin-top: 30rpx; padding: 0 20rpx; border-radius: 7rpx; } .pick-view { padding: 20rpx 0; } .pick-view image { display: block; width: 30rpx; height: 30rpx; margin-left: 20rpx; } .pick-view view:nth-child(1) { display: flex; align-items: center; justify-content: space-between; } .flex-left { flex: 1; } .flex-left view { justify-content: flex-end !important; } .Underline { border-bottom: 1rpx solid #ededed; } .pick-time { font-size: 27rpx; } </style>
date.js
// 开始日期 const start_date = [ [],//年 [],//月 [],//日 [//时 { time:'00', name:'0时' }, { time:'01', name:'1时' }, { time:'02', name:'2时' }, { time:'03', name:'3时' }, { time:'04', name:'4时' }, { time:'05', name:'5时' }, { time:'06', name:'6时' }, { time:'07', name:'7时' }, { time:'08', name:'8时' }, { time:'09', name:'9时' }, { time:'10', name:'10时' }, { time:'11', name:'11时' }, { time:'12', name:'12时' }, { time:'13', name:'13时' }, { time:'14', name:'14时' }, { time:'15', name:'15时' }, { time:'16', name:'16时' }, { time:'17', name:'17时' }, { time:'18', name:'18时' }, { time:'19', name:'19时' }, { time:'20', name:'20时' }, { time:'21', name:'21时' }, { time:'22', name:'22时' }, { time:'23', name:'23时' } ], [//分 { time:'00', name:'0分' }, { time:'01', name:'1分' }, { time:'02', name:'2分' }, { time:'03', name:'3分' }, { time:'04', name:'4分' }, { time:'05', name:'5分' }, { time:'06', name:'6分' }, { time:'07', name:'7分' }, { time:'08', name:'8分' }, { time:'09', name:'9分' }, { time:'10', name:'10分' }, { time:'11', name:'11分' }, { time:'12', name:'12分' }, { time:'13', name:'13分' }, { time:'14', name:'14分' }, { time:'15', name:'15分' }, { time:'16', name:'16分' }, { time:'17', name:'17分' }, { time:'18', name:'18分' }, { time:'19', name:'19分' }, { time:'20', name:'20分' }, { time:'21', name:'21分' }, { time:'22', name:'22分' }, { time:'23', name:'23分' }, { time:'24', name:'24分' }, { time:'25', name:'25分' }, { time:'26', name:'26分' }, { time:'27', name:'27分' }, { time:'28', name:'28分' }, { time:'29', name:'29分' }, { time:'30', name:'30分' }, { time:'31', name:'31分' }, { time:'32', name:'32分' }, { time:'33', name:'33分' }, { time:'34', name:'34分' }, { time:'35', name:'35分' }, { time:'36', name:'36分' }, { time:'37', name:'37分' }, { time:'38', name:'38分' }, { time:'39', name:'39分' }, { time:'40', name:'40分' }, { time:'41', name:'41分' }, { time:'42', name:'42分' }, { time:'43', name:'43分' }, { time:'44', name:'44分' }, { time:'45', name:'45分' }, { time:'46', name:'46分' }, { time:'47', name:'47分' }, { time:'48', name:'48分' }, { time:'49', name:'49分' }, { time:'50', name:'50分' }, { time:'51', name:'51分' }, { time:'52', name:'52分' }, { time:'53', name:'53分' }, { time:'54', name:'54分' }, { time:'55', name:'55分' }, { time:'56', name:'56分' }, { time:'57', name:'57分' }, { time:'58', name:'58分' }, { time:'59', name:'59分' } ], ] // 结束日期 const end_date = [ [],//年 [],//月 [],//日 [//时 { time:'00', name:'0时' }, { time:'01', name:'1时' }, { time:'02', name:'2时' }, { time:'03', name:'3时' }, { time:'04', name:'4时' }, { time:'05', name:'5时' }, { time:'06', name:'6时' }, { time:'07', name:'7时' }, { time:'08', name:'8时' }, { time:'09', name:'9时' }, { time:'10', name:'10时' }, { time:'11', name:'11时' }, { time:'12', name:'12时' }, { time:'13', name:'13时' }, { time:'14', name:'14时' }, { time:'15', name:'15时' }, { time:'16', name:'16时' }, { time:'17', name:'17时' }, { time:'18', name:'18时' }, { time:'19', name:'19时' }, { time:'20', name:'20时' }, { time:'21', name:'21时' }, { time:'22', name:'22时' }, { time:'23', name:'23时' } ], [//分 { time:'00', name:'0分' }, { time:'01', name:'1分' }, { time:'02', name:'2分' }, { time:'03', name:'3分' }, { time:'04', name:'4分' }, { time:'05', name:'5分' }, { time:'06', name:'6分' }, { time:'07', name:'7分' }, { time:'08', name:'8分' }, { time:'09', name:'9分' }, { time:'10', name:'10分' }, { time:'11', name:'11分' }, { time:'12', name:'12分' }, { time:'13', name:'13分' }, { time:'14', name:'14分' }, { time:'15', name:'15分' }, { time:'16', name:'16分' }, { time:'17', name:'17分' }, { time:'18', name:'18分' }, { time:'19', name:'19分' }, { time:'20', name:'20分' }, { time:'21', name:'21分' }, { time:'22', name:'22分' }, { time:'23', name:'23分' }, { time:'24', name:'24分' }, { time:'25', name:'25分' }, { time:'26', name:'26分' }, { time:'27', name:'27分' }, { time:'28', name:'28分' }, { time:'29', name:'29分' }, { time:'30', name:'30分' }, { time:'31', name:'31分' }, { time:'32', name:'32分' }, { time:'33', name:'33分' }, { time:'34', name:'34分' }, { time:'35', name:'35分' }, { time:'36', name:'36分' }, { time:'37', name:'37分' }, { time:'38', name:'38分' }, { time:'39', name:'39分' }, { time:'40', name:'40分' }, { time:'41', name:'41分' }, { time:'42', name:'42分' }, { time:'43', name:'43分' }, { time:'44', name:'44分' }, { time:'45', name:'45分' }, { time:'46', name:'46分' }, { time:'47', name:'47分' }, { time:'48', name:'48分' }, { time:'49', name:'49分' }, { time:'50', name:'50分' }, { time:'51', name:'51分' }, { time:'52', name:'52分' }, { time:'53', name:'53分' }, { time:'54', name:'54分' }, { time:'55', name:'55分' }, { time:'56', name:'56分' }, { time:'57', name:'57分' }, { time:'58', name:'58分' }, { time:'59', name:'59分' } ], ] export {start_date,end_date}
ca-time.js
import moment from 'moment' moment.locale('zh-cn'); import {start_date,end_date} from '@/Acc-config/date.js' let current = ()=>{ /* 进入页面获取当前的年月日 */ const C_year = moment().format('YYYY')//当前年 const C_month = moment().format('M')//当前月 const C_day = moment().format('D')//当前日 const N_year = moment().add(1,'year').format('YYYY')//明年 // 获取今年和明年------------------年 start_date[0] = [{time:C_year,name:C_year + '年'},{time:N_year,name:N_year + '年'}] end_date[0] = [{time:C_year,name:C_year + '年'},{time:N_year,name:N_year + '年'}] // 获取从当年月开始计算:本月到12月----------月 for(let w = C_month; w <= 12; w++){ start_date[1].push({time:Number(w),name:w + '月'}) end_date[1].push({time:Number(w),name:w + '月'}) } // 获取今年每个月的天数:从当前月开始计算2022/05-------日 const Days = moment(C_year + '/' + C_month, 'YYYY/M').daysInMonth()//当前月的天数 for(let i = C_day; i <= Days; i++){ start_date[2].push({time:Number(i),name:i + '日'}) end_date[2].push({time:Number(i),name:i + '日'}) } } // 计算本月或1月到12月 let months = (years)=>{ const C_month = moment().format('M')//当前月 const N_year = moment().add(1,'year').format('YYYY')//明年 let MONTH = years == N_year ? 1 : C_month let mohth_data = [] for(let w = Number(MONTH); w <= 12; w++){ mohth_data.push({time:Number(w),name:w + '月'}) } return mohth_data } // 计算本月的天数 let codays = (years)=>{ const C_year = moment().format('YYYY')//当前年 const C_month = moment().format('M')//当前月 const C_day = moment().format('D')//当前日 // 如果滚动在当前年月,那么"日"要从今天算起 let INIT = years.year == C_year && years.month == C_month ? C_day : 1 const new_data = [] const Days = moment(years.year + '/' + years.month, 'YYYY/M').daysInMonth()//当月的天数 for(let i = Number(INIT); i <= Days; i++){ new_data.push({time:Number(i),name:i + '日'}) } return new_data } export {current,months,codays}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。