当前位置:网站首页>uniapp时间组件封装年-月-日-时-分-秒
uniapp时间组件封装年-月-日-时-分-秒
2022-08-05 07:25:00 【小布丁儿Y】
在做项目时 用到可以选择时分秒的组件 百度了很多 跟自己的需求不太符合 于是请求公司前端大佬封装了一个可选择的年-月-日-时-分-秒组件
这是我要实现的效果:

单独建一个文件夹 在需要用到的页面引入
<!--
用途:日期时间拨盘选择
参数:
title
说明:当前组件的title名称
类型:String
默认值:弹窗信息
type
说明:日期时间筛选类型
类型:String
默认值:ymd
minDate
说明:最小选择时间
类型:String
默认值:2000-01-01 00:00:00
maxDate
说明:最大选择时间
类型:String
默认值:3000-01-01 00:00:00
initDate
说明:初始默认时间
类型:String
默认值:2020-05-20 00:00:00
confirmButtonText
说明:确认按钮文本
类型:String
默认值:确认
:showConfirmButton
说明:是否显示确认按钮
类型:Boolean
默认值:true
@confirmFunction
说明:确认按钮操作事件
类型:Function
@cancelFunction
说明:取消按钮操作事件
类型:Function
-->
<template>
<!-- <view class="datePicker" ref="datePicker" catchtouchmove="true"> -->
<view class="datePicker" ref="datePicker">
<uni-popup ref="popup" type="bottom">
<view class="popupBox">
<view class="dtitle">
<view class="name">{ {title}}</view>
<view class="close" @click="cancelFunction();"></view>
</view>
<picker-view
@change="onChange($event)"
class="picker-view"
@pickstart="pickStart($event)"
@pickend="pickEnd()"
:value="dateValue"
>
<picker-view-column v-for="(item,index) in data" :key="index">
<view class="item" v-for="(item1,index1) in item.data" :key="index1">{ {item1}}{ {item.$name}}</view>
</picker-view-column>
</picker-view>
<view class="operate">
<view class="btn confirm" :class="{'cur':isDataFinish}" @click="confirmFunction();">{ {confirmButtonText}}</view>
</view>
</view>
</uni-popup>
</view>
</template>
<script>
import uniPopup from '@/components/uni-popup/uni-popup.vue';
export default {
components:{
uniPopup
},
data () {
return {
data:[], //初始化时间维度
changeObj:[], //初始化选中的数据
isDataFinish:true, //初始化数据是否选择完毕
normalDate:"", //初始化默认选择的时间
dateValue:[], //初始化拨盘选择默认值
tempChangeObj:[], //初始化滚动选中
}
},
props:{
// title名称,类型 String 默认 ""
"title":{
type:String,
default:"请选择时间"
},
// 拨盘类型,类型 String 默认 "yy-mm-dd"
"type":{
type:String,
default:"yy-mm-dd"
},
// 最小选择时间
"minDate":{
type:String,
default:"2000-01-01 00:00:00"
},
// 最小选择时间
"maxDate":{
type:String,
default:"3000-01-01 00:00:00"
},
// 初始默认时间
"initDate":{
type:String,
default:""
},
// 确认按钮名称 类型 String 默认 "确认"
'confirmButtonText':{
type:String,
default:'确认'
},
// 确认方法名-兼容微信小程序无法获取父组件是否传递过来参数的补丁
'mpWeixinConfirm':{
type:String
},
// 确认方法名-兼容微信小程序无法获取父组件是否传递过来参数的补丁
'mpWeixinCancel':{
type:String
}
},
methods: {
// open 开启弹窗方法
open(){
this.data=[];
// 重置默认选择的日期时间
if(!this.initDate||new Date(this.initDate).getTime()<new Date(this.minDate).getTime()||new Date(this.initDate).getTime()>new Date(this.maxDate).getTime()){
this.normalDate = this.minDate;
}else{
this.normalDate = this.initDate;
}
// 获取类型
switch(this.type){
case "yy":
// 追加年数据
var year = {};
year.name = "year";
year.$name = "年"
year.data = [];
var yearInterval = this.maxDate.split("-")[0] - this.minDate.split("-")[0];
for(let a=0;a<=yearInterval;a++){
year.data.push(parseInt(this.minDate.split("-")[0])+a);
if(this.normalDate.split("-")[0]==(parseInt(this.minDate.split("-")[0])+a)){
this.dateValue.push(a)
}
}
this.tempChangeObj = this.dateValue;
this.data.push(year);
break;
case "yy-mm":
//追加年数据
var year = {};
year.name = "year";
year.$name = "年"
year.data = [];
var yearInterval = this.maxDate.split("-")[0] - this.minDate.split("-")[0];
for(let a=0;a<=yearInterval;a++){
year.data.push(parseInt(this.minDate.split("-")[0])+a);
if(this.normalDate.split("-")[0]==(parseInt(this.minDate.split("-")[0])+a)){
this.dateValue.push(a)
}
}
this.data.push(year);
// 追加月数据
var month = {};
month.name = "month";
month.$name = "月";
month.data = [];
// 判断年份如果在同一年
var monthInterval = 0;
if(yearInterval>0){
monthInterval = 12;
for(let b=1;b<=monthInterval;b++){
month.data.push(b<10?("0"+b):b);
if(this.normalDate.split(" ")[0].split("-")[1]==(b<10?("0"+b):b)){
this.dateValue.push(b-1)
}
}
}else{
monthInterval = this.maxDate.split(" ")[0].split("-")[1] - this.minDate.split(" ")[0].split("-")[1];
for(let b=0;b<=monthInterval;b++){
month.data.push((parseInt(this.minDate.split(" ")[0].split("-")[1])+b)<10?("0"+(parseInt(this.minDate.split(" ")[0].split("-")[1])+b)):(parseInt(this.minDate.split(" ")[0].split("-")[1])+b));
if(parseInt(this.normalDate.split(" ")[0].split("-")[1])==parseInt(this.minDate.split(" ")[0].split("-")[1])+b){
this.dateValue.push(b)
}
}
}
this.tempChangeObj = this.dateValue;
this.data.push(month);
break;
case "yy-mm-dd":
//追加年数据
var year = {};
year.name = "year";
year.$name = "年"
year.data = [];
var yearInterval = this.maxDate.split("-")[0] - this.minDate.split("-")[0];
for(let a=0;a<=yearInterval;a++){
year.data.push(parseInt(this.minDate.split("-")[0])+a);
if(this.normalDate.split("-")[0]==(parseInt(this.minDate.split("-")[0])+a)){
this.dateValue.push(a)
}
}
this.data.push(year);
// 追加月数据
var month = {};
month.name = "month";
month.$name = "月";
month.data = [];
// 判断年份如果在同一年
var monthInterval = 0;
if(yearInterval>0){
monthInterval = 12;
for(let b=1;b<=monthInterval;b++){
month.data.push(b<10?("0"+b):b);
if(this.normalDate.split(" ")[0].split("-")[1]==(b<10?("0"+b):b)){
this.dateValue.push(b-1)
}
}
}else{
monthInterval = this.maxDate.split(" ")[0].split("-")[1] - this.minDate.split(" ")[0].split("-")[1];
for(let b=0;b<=monthInterval;b++){
month.data.push((parseInt(this.minDate.split(" ")[0].split("-")[1])+b)<10?("0"+(parseInt(this.minDate.split(" ")[0].split("-")[1])+b)):(parseInt(this.minDate.split(" ")[0].split("-")[1])+b));
if(parseInt(this.normalDate.split(" ")[0].split("-")[1])==parseInt(this.minDate.split(" ")[0].split("-")[1])+b){
this.dateValue.push(b)
}
}
}
this.data.push(month);
// 追加日数据
var day = {};
day.name = "day";
day.$name = "日";
day.data = [];
// 判断年月是否在同一年同一月
var dayInterval = 0;
if(yearInterval==0&&(this.maxDate.split(" ")[0].split("-")[1] - this.minDate.split(" ")[0].split("-")[1]==0)){
dayInterval = this.maxDate.split(" ")[0].split("-")[2] - this.minDate.split(" ")[0].split("-")[2];
for(let c=0;c<=dayInterval;c++){
day.data.push((parseInt(this.minDate.split(" ")[0].split("-")[2])+c)<10?("0"+(parseInt(this.minDate.split(" ")[0].split("-")[2])+c)):(parseInt(this.minDate.split(" ")[0].split("-")[2])+c));
if(parseInt(this.normalDate.split(" ")[0].split("-")[2])==parseInt(this.minDate.split(" ")[0].split("-")[2])+c){
this.dateValue.push(c)
}
}
}else{
var tempMonth = this.normalDate.split(" ")[0].split("-")[1];
// 判断是否为2月
if(tempMonth==2){
// 判断当前选择,是否闰年
if(this.normalDate.split(" ")[0].split("-")[0] % 4 == 0 && !(this.normalDate.split(" ")[0].split("-")[0] % 100 == 0) || this.normalDate.split(" ")[0].split("-")[0] % 400 == 0) {
dayInterval = 29;
}else{
dayInterval = 28;
}
}else if(tempMonth==1||tempMonth==3||tempMonth==5||tempMonth==7||tempMonth==8||tempMonth==10||tempMonth==12){
dayInterval = 31;
}else{
dayInterval = 30;
}
for(let c=1;c<=dayInterval;c++){
day.data.push(c<10?("0"+c):c);
if(this.normalDate.split(" ")[0].split("-")[2]==(c<10?("0"+c):c)){
this.dateValue.push(c-1)
}
}
}
this.tempChangeObj = this.dateValue;
this.data.push(day);
break;
case "yy-mm-dd hh":
//追加年数据
var year = {};
year.name = "year";
year.$name = "年"
year.data = [];
var yearInterval = this.maxDate.split("-")[0] - this.minDate.split("-")[0];
for(let a=0;a<=yearInterval;a++){
year.data.push(parseInt(this.minDate.split("-")[0])+a);
if(this.normalDate.split("-")[0]==(parseInt(this.minDate.split("-")[0])+a)){
this.dateValue.push(a)
}
}
this.data.push(year);
// 追加月数据
var month = {};
month.name = "month";
month.$name = "月";
month.data = [];
// 判断年份如果在同一年
var monthInterval = 0;
if(yearInterval>0){
monthInterval = 12;
for(let b=1;b<=monthInterval;b++){
month.data.push(b<10?("0"+b):b);
if(this.normalDate.split(" ")[0].split("-")[1]==(b<10?("0"+b):b)){
this.dateValue.push(b-1)
}
}
}else{
monthInterval = this.maxDate.split(" ")[0].split("-")[1] - this.minDate.split(" ")[0].split("-")[1];
for(let b=0;b<=monthInterval;b++){
month.data.push((parseInt(this.minDate.split(" ")[0].split("-")[1])+b)<10?("0"+(parseInt(this.minDate.split(" ")[0].split("-")[1])+b)):(parseInt(this.minDate.split(" ")[0].split("-")[1])+b));
if(parseInt(this.normalDate.split(" ")[0].split("-")[1])==parseInt(this.minDate.split(" ")[0].split("-")[1])+b){
this.dateValue.push(b)
}
}
}
this.data.push(month);
// 追加日数据
var day = {};
day.name = "day";
day.$name = "日";
day.data = [];
// 判断年月是否在同一年同一月
var dayInterval = 0;
if(yearInterval==0&&(this.maxDate.split(" ")[0].split("-")[1] - this.minDate.split(" ")[0].split("-")[1]==0)){
dayInterval = this.maxDate.split(" ")[0].split("-")[2] - this.minDate.split(" ")[0].split("-")[2];
for(let c=0;c<=dayInterval;c++){
day.data.push((parseInt(this.minDate.split(" ")[0].split("-")[2])+c)<10?("0"+(parseInt(this.minDate.split(" ")[0].split("-")[2])+c)):(parseInt(this.minDate.split(" ")[0].split("-")[2])+c));
if(parseInt(this.normalDate.split(" ")[0].split("-")[2])==parseInt(this.minDate.split(" ")[0].split("-")[2])+c){
this.dateValue.push(c)
}
}
}else{
var tempMonth = this.normalDate.split(" ")[0].split("-")[1];
// 判断是否为2月
if(tempMonth==2){
// 判断当前选择,是否闰年
if(this.normalDate.split(" ")[0].split("-")[0] % 4 == 0 && !(this.normalDate.split(" ")[0].split("-")[0] % 100 == 0) || this.normalDate.split(" ")[0].split("-")[0] % 400 == 0) {
dayInterval = 29;
}else{
dayInterval = 28;
}
}else if(tempMonth==1||tempMonth==3||tempMonth==5||tempMonth==7||tempMonth==8||tempMonth==10||tempMonth==12){
dayInterval = 31;
}else{
dayInterval = 30;
}
for(let c=1;c<=dayInterval;c++){
day.data.push(c<10?("0"+c):c);
if(this.normalDate.split(" ")[0].split("-")[2]==(c<10?("0"+c):c)){
this.dateValue.push(c-1)
}
}
}
this.data.push(day);
// 追加时数据
var hour = {};
hour.name = "hour";
hour.$name = "时";
hour.data = [];
// 判断年月日是否一致
var hourInterval = 0;
if(yearInterval==0&&(this.maxDate.split(" ")[0].split("-")[1] - this.minDate.split(" ")[0].split("-")[1]==0)&&(this.maxDate.split(" ")[0].split("-")[2] - this.minDate.split(" ")[0].split("-")[2]==0)){
hourInterval = this.maxDate.split(" ")[1].split(":")[0] - this.minDate.split(" ")[1].split(":")[0];
for(let d=0;d<=hourInterval;d++){
hour.data.push((parseInt(this.minDate.split(" ")[1].split(":")[0])+d)<10?("0"+(parseInt(this.minDate.split(" ")[1].split(":")[0])+d)):(parseInt(this.minDate.split(" ")[1].split(":")[0])+d));
if(parseInt(this.normalDate.split(" ")[1].split(":")[0])==parseInt(this.minDate.split(" ")[1].split(":")[0])+d){
this.dateValue.push(d)
}
}
}else{
hourInterval = 23
for(let d=0;d<=hourInterval;d++){
hour.data.push(d<10?("0"+d):d);
if(this.normalDate.split(" ")[1].split(":")[0]==(d<10?("0"+d):d)){
this.dateValue.push(d)
}
}
}
this.tempChangeObj = this.dateValue;
this.data.push(hour);
break;
case "yy-mm-dd hh:mm":
//追加年数据
var year = {};
year.name = "year";
year.$name = "年"
year.data = [];
var yearInterval = this.maxDate.split("-")[0] - this.minDate.split("-")[0];
for(let a=0;a<=yearInterval;a++){
year.data.push(parseInt(this.minDate.split("-")[0])+a);
if(this.normalDate.split("-")[0]==(parseInt(this.minDate.split("-")[0])+a)){
this.dateValue.push(a)
}
}
this.data.push(year);
// 追加月数据
var month = {};
month.name = "month";
month.$name = "月";
month.data = [];
// 判断年份如果在同一年
var monthInterval = 0;
if(yearInterval>0){
monthInterval = 12;
for(let b=1;b<=monthInterval;b++){
month.data.push(b<10?("0"+b):b);
if(this.normalDate.split(" ")[0].split("-")[1]==(b<10?("0"+b):b)){
this.dateValue.push(b-1)
}
}
}else{
monthInterval = this.maxDate.split(" ")[0].split("-")[1] - this.minDate.split(" ")[0].split("-")[1];
for(let b=0;b<=monthInterval;b++){
month.data.push((parseInt(this.minDate.split(" ")[0].split("-")[1])+b)<10?("0"+(parseInt(this.minDate.split(" ")[0].split("-")[1])+b)):(parseInt(this.minDate.split(" ")[0].split("-")[1])+b));
if(parseInt(this.normalDate.split(" ")[0].split("-")[1])==parseInt(this.minDate.split(" ")[0].split("-")[1])+b){
this.dateValue.push(b)
}
}
}
this.data.push(month);
// 追加日数据
var day = {};
day.name = "day";
day.$name = "日";
day.data = [];
// 判断年月是否在同一年同一月
var dayInterval = 0;
if(yearInterval==0&&(this.maxDate.split(" ")[0].split("-")[1] - this.minDate.split(" ")[0].split("-")[1]==0)){
dayInterval = this.maxDate.split(" ")[0].split("-")[2] - this.minDate.split(" ")[0].split("-")[2];
for(let c=0;c<=dayInterval;c++){
day.data.push((parseInt(this.minDate.split(" ")[0].split("-")[2])+c)<10?("0"+(parseInt(this.minDate.split(" ")[0].split("-")[2])+c)):(parseInt(this.minDate.split(" ")[0].split("-")[2])+c));
if(parseInt(this.normalDate.split(" ")[0].split("-")[2])==parseInt(this.minDate.split(" ")[0].split("-")[2])+c){
this.dateValue.push(c)
}
}
}else{
var tempMonth = this.normalDate.split(" ")[0].split("-")[1];
// 判断是否为2月
if(tempMonth==2){
// 判断当前选择,是否闰年
if(this.normalDate.split(" ")[0].split("-")[0] % 4 == 0 && !(this.normalDate.split(" ")[0].split("-")[0] % 100 == 0) || this.normalDate.split(" ")[0].split("-")[0] % 400 == 0) {
dayInterval = 29;
}else{
dayInterval = 28;
}
}else if(tempMonth==1||tempMonth==3||tempMonth==5||tempMonth==7||tempMonth==8||tempMonth==10||tempMonth==12){
dayInterval = 31;
}else{
dayInterval = 30;
}
for(let c=1;c<=dayInterval;c++){
day.data.push(c<10?("0"+c):c);
if(this.normalDate.split(" ")[0].split("-")[2]==(c<10?("0"+c):c)){
this.dateValue.push(c-1)
}
}
}
this.data.push(day);
// 追加时数据
var hour = {};
hour.name = "hour";
hour.$name = "时";
hour.data = [];
// 判断年月日是否一致
var hourInterval = 0;
if(yearInterval==0&&(this.maxDate.split(" ")[0].split("-")[1] - this.minDate.split(" ")[0].split("-")[1]==0)&&(this.maxDate.split(" ")[0].split("-")[2] - this.minDate.split(" ")[0].split("-")[2]==0)){
hourInterval = this.maxDate.split(" ")[1].split(":")[0] - this.minDate.split(" ")[1].split(":")[0];
for(let d=0;d<=hourInterval;d++){
hour.data.push((parseInt(this.minDate.split(" ")[1].split(":")[0])+d)<10?("0"+(parseInt(this.minDate.split(" ")[1].split(":")[0])+d)):(parseInt(this.minDate.split(" ")[1].split(":")[0])+d));
if(parseInt(this.normalDate.split(" ")[1].split(":")[0])==parseInt(this.minDate.split(" ")[1].split(":")[0])+d){
this.dateValue.push(d)
}
}
}else{
hourInterval = 23
for(let d=0;d<=hourInterval;d++){
hour.data.push(d<10?("0"+d):d);
if(this.normalDate.split(" ")[1].split(":")[0]==(d<10?("0"+d):d)){
this.dateValue.push(d)
}
}
}
this.data.push(hour);
// 追加分数据
var minute = {};
minute.name = "minute";
minute.$name = "分";
minute.data = [];
// 判断年月日时是否一致
var minuteInterval = 0;
if(yearInterval==0&&(this.maxDate.split(" ")[0].split("-")[1] - this.minDate.split(" ")[0].split("-")[1]==0)&&(this.maxDate.split(" ")[0].split("-")[2] - this.minDate.split(" ")[0].split("-")[2]==0)&&(this.maxDate.split(" ")[1].split(":")[0] - this.minDate.split(" ")[1].split(":")[0]==0)){
minuteInterval = this.maxDate.split(" ")[1].split(":")[1] - this.minDate.split(" ")[1].split(":")[1];
for(let e=0;e<=minuteInterval;e++){
minute.data.push((parseInt(this.minDate.split(" ")[1].split(":")[1])+e)<10?("0"+(parseInt(this.minDate.split(" ")[1].split(":")[1])+e)):(parseInt(this.minDate.split(" ")[1].split(":")[1])+e));
if(parseInt(this.normalDate.split(" ")[1].split(":")[1])==parseInt(this.minDate.split(" ")[1].split(":")[1])+e){
this.dateValue.push(e)
}
}
}else{
minuteInterval = 59
for(let e=0;e<=minuteInterval;e++){
minute.data.push(e<10?("0"+e):e);
if(this.normalDate.split(" ")[1].split(":")[1]==(e<10?("0"+e):e)){
this.dateValue.push(e)
}
}
}
this.tempChangeObj = this.dateValue;
this.data.push(minute);
break;
case "yy-mm-dd hh:mm:ss":
//追加年数据
var year = {};
year.name = "year";
year.$name = "年"
year.data = [];
var yearInterval = this.maxDate.split("-")[0] - this.minDate.split("-")[0];
for(let a=0;a<=yearInterval;a++){
year.data.push(parseInt(this.minDate.split("-")[0])+a);
if(this.normalDate.split("-")[0]==(parseInt(this.minDate.split("-")[0])+a)){
this.dateValue.push(a)
}
}
this.data.push(year);
// 追加月数据
var month = {};
month.name = "month";
month.$name = "月";
month.data = [];
// 判断年份如果在同一年
var monthInterval = 0;
if(yearInterval>0){
monthInterval = 12;
for(let b=1;b<=monthInterval;b++){
month.data.push(b<10?("0"+b):b);
if(this.normalDate.split(" ")[0].split("-")[1]==(b<10?("0"+b):b)){
this.dateValue.push(b-1)
}
}
}else{
monthInterval = this.maxDate.split(" ")[0].split("-")[1] - this.minDate.split(" ")[0].split("-")[1];
for(let b=0;b<=monthInterval;b++){
month.data.push((parseInt(this.minDate.split(" ")[0].split("-")[1])+b)<10?("0"+(parseInt(this.minDate.split(" ")[0].split("-")[1])+b)):(parseInt(this.minDate.split(" ")[0].split("-")[1])+b));
if(parseInt(this.normalDate.split(" ")[0].split("-")[1])==parseInt(this.minDate.split(" ")[0].split("-")[1])+b){
this.dateValue.push(b)
}
}
}
this.data.push(month);
// 追加日数据
var day = {};
day.name = "day";
day.$name = "日";
day.data = [];
// 判断年月是否在同一年同一月
var dayInterval = 0;
if(yearInterval==0&&(this.maxDate.split(" ")[0].split("-")[1] - this.minDate.split(" ")[0].split("-")[1]==0)){
dayInterval = this.maxDate.split(" ")[0].split("-")[2] - this.minDate.split(" ")[0].split("-")[2];
for(let c=0;c<=dayInterval;c++){
day.data.push((parseInt(this.minDate.split(" ")[0].split("-")[2])+c)<10?("0"+(parseInt(this.minDate.split(" ")[0].split("-")[2])+c)):(parseInt(this.minDate.split(" ")[0].split("-")[2])+c));
if(parseInt(this.normalDate.split(" ")[0].split("-")[2])==parseInt(this.minDate.split(" ")[0].split("-")[2])+c){
this.dateValue.push(c)
}
}
}else{
var tempMonth = this.normalDate.split(" ")[0].split("-")[1];
// 判断是否为2月
if(tempMonth==2){
// 判断当前选择,是否闰年
if(this.normalDate.split(" ")[0].split("-")[0] % 4 == 0 && !(this.normalDate.split(" ")[0].split("-")[0] % 100 == 0) || this.normalDate.split(" ")[0].split("-")[0] % 400 == 0) {
dayInterval = 29;
}else{
dayInterval = 28;
}
}else if(tempMonth==1||tempMonth==3||tempMonth==5||tempMonth==7||tempMonth==8||tempMonth==10||tempMonth==12){
dayInterval = 31;
}else{
dayInterval = 30;
}
for(let c=1;c<=dayInterval;c++){
day.data.push(c<10?("0"+c):c);
if(this.normalDate.split(" ")[0].split("-")[2]==(c<10?("0"+c):c)){
this.dateValue.push(c-1)
}
}
}
this.data.push(day);
// 追加时数据
var hour = {};
hour.name = "hour";
hour.$name = "时";
hour.data = [];
// 判断年月日是否一致
var hourInterval = 0;
if(yearInterval==0&&(this.maxDate.split(" ")[0].split("-")[1] - this.minDate.split(" ")[0].split("-")[1]==0)&&(this.maxDate.split(" ")[0].split("-")[2] - this.minDate.split(" ")[0].split("-")[2]==0)){
hourInterval = this.maxDate.split(" ")[1].split(":")[0] - this.minDate.split(" ")[1].split(":")[0];
for(let d=0;d<=hourInterval;d++){
hour.data.push((parseInt(this.minDate.split(" ")[1].split(":")[0])+d)<10?("0"+(parseInt(this.minDate.split(" ")[1].split(":")[0])+d)):(parseInt(this.minDate.split(" ")[1].split(":")[0])+d));
if(parseInt(this.normalDate.split(" ")[1].split(":")[0])==parseInt(this.minDate.split(" ")[1].split(":")[0])+d){
this.dateValue.push(d)
}
}
}else{
hourInterval = 23
for(let d=0;d<=hourInterval;d++){
hour.data.push(d<10?("0"+d):d);
if(this.normalDate.split(" ")[1].split(":")[0]==(d<10?("0"+d):d)){
this.dateValue.push(d)
}
}
}
this.data.push(hour);
// 追加分数据
var minute = {};
minute.name = "minute";
minute.$name = "分";
minute.data = [];
// 判断年月日时是否一致
var minuteInterval = 0;
if(yearInterval==0&&(this.maxDate.split(" ")[0].split("-")[1] - this.minDate.split(" ")[0].split("-")[1]==0)&&(this.maxDate.split(" ")[0].split("-")[2] - this.minDate.split(" ")[0].split("-")[2]==0)&&(this.maxDate.split(" ")[1].split(":")[0] - this.minDate.split(" ")[1].split(":")[0]==0)){
minuteInterval = this.maxDate.split(" ")[1].split(":")[1] - this.minDate.split(" ")[1].split(":")[1];
for(let e=0;e<=minuteInterval;e++){
minute.data.push((parseInt(this.minDate.split(" ")[1].split(":")[1])+e)<10?("0"+(parseInt(this.minDate.split(" ")[1].split(":")[1])+e)):(parseInt(this.minDate.split(" ")[1].split(":")[1])+e));
if(parseInt(this.normalDate.split(" ")[1].split(":")[1])==parseInt(this.minDate.split(" ")[1].split(":")[1])+e){
this.dateValue.push(e)
}
}
}else{
minuteInterval = 59
for(let e=0;e<=minuteInterval;e++){
minute.data.push(e<10?("0"+e):e);
if(this.normalDate.split(" ")[1].split(":")[1]==(e<10?("0"+e):e)){
this.dateValue.push(e)
}
}
}
this.data.push(minute);
// 追加秒数据
var seconds = {};
seconds.name = "seconds";
seconds.$name = "秒";
seconds.data = [];
// 判断年月日时分是否一致
var secondsInterval = 0;
if(yearInterval==0&&(this.maxDate.split(" ")[0].split("-")[1] - this.minDate.split(" ")[0].split("-")[1]==0)&&(this.maxDate.split(" ")[0].split("-")[2] - this.minDate.split(" ")[0].split("-")[2]==0)&&(this.maxDate.split(" ")[1].split(":")[0] - this.minDate.split(" ")[1].split(":")[0]==0)&&(this.maxDate.split(" ")[1].split(":")[1] - this.minDate.split(" ")[1].split(":")[1]==0)){
secondsInterval = this.maxDate.split(" ")[1].split(":")[2] - this.minDate.split(" ")[1].split(":")[2];
for(let f=0;f<=secondsInterval;f++){
seconds.data.push((parseInt(this.minDate.split(" ")[1].split(":")[2])+f)<10?("0"+(parseInt(this.minDate.split(" ")[1].split(":")[2])+f)):(parseInt(this.minDate.split(" ")[1].split(":")[2])+f));
if(parseInt(this.normalDate.split(" ")[1].split(":")[2])==parseInt(this.minDate.split(" ")[1].split(":")[2])+f){
this.dateValue.push(f)
}
}
}else{
secondsInterval = 59
for(let f=0;f<=secondsInterval;f++){
seconds.data.push(f<10?("0"+f):f);
if(this.normalDate.split(" ")[1].split(":")[2]==(f<10?("0"+f):f)){
this.dateValue.push(f)
}
}
}
this.tempChangeObj = this.dateValue;
this.data.push(seconds);
break;
}
this.$refs.popup.open();
},
// close 关闭弹窗方法
close(){
this.$refs.popup.close();
},
// 拨盘选择
onChange($event){
// 此处请注意,微信小程序返回默认值,h5端不会返回
var tempArray = $event.detail.value;
// 判断年月是否改变
if((tempArray[0]!=this.tempChangeObj[0]||tempArray[1]!=this.tempChangeObj[1])&&(this.data[0].data.length>1||this.data[1].data.length>1)){
var daySize = 0;
// 判断拨盘类型
if((this.type=="yy-mm-dd"||this.type=="yy-mm-dd hh"||this.type=="yy-mm-dd hh:mm"||this.type=="yy-mm-dd hh:mm:ss")&&parseInt(this.data[1].data[tempArray[1]])==2){
// 判断当前选择,是否闰年
if(parseInt(this.data[0].data[tempArray[0]]) % 4 == 0 && !(parseInt(this.data[0].data[tempArray[0]]) % 100 == 0) || parseInt(this.data[0].data[tempArray[0]]) % 400 == 0) {
daySize = 29;
}else{
daySize = 28;
}
}else if((this.type=="yy-mm-dd"||this.type=="yy-mm-dd hh"||this.type=="yy-mm-dd hh:mm"||this.type=="yy-mm-dd hh:mm:ss")&&(parseInt(this.data[1].data[tempArray[1]])==1||parseInt(this.data[1].data[tempArray[1]])==3||parseInt(this.data[1].data[tempArray[1]])==5||parseInt(this.data[1].data[tempArray[1]])==7||parseInt(this.data[1].data[tempArray[1]])==8||parseInt(this.data[1].data[tempArray[1]])==10||parseInt(this.data[1].data[tempArray[1]])==12)){
daySize = 31;
}else{
daySize = 30;
}
var temDay = [];
for(let x=1;x<=daySize;x++){
temDay.push(x<10?("0"+x):x)
}
this.data[2].data = temDay;
}
this.tempChangeObj=tempArray;
},
// 拨盘滚动开始
pickStart($event){
this.isDataFinish = false;
},
// 拨盘滚动结束
pickEnd(){
this.isDataFinish = true;
},
// 执行组件确认按钮绑定方法
confirmFunction(){
this.changeObj = [];
// 组装输出数据
for(let x=0;x<this.data.length;x++){
this.changeObj.push({
name:this.data[x].name,
$name:this.data[x].$name,
value:this.data[x].data[this.tempChangeObj[x]]
})
}
console.log(this.changeObj)
// 判断是否正在滚动选择中
if(!this.isDataFinish){
return false;
}
// 执行默认方法
this.$refs.popup.close();
// #ifdef MP-WEIXIN
// 判断父组件是否传递过来确认方法
// 次数区别处理,用以为 this.$listeners在微信小程序内获取为{}对象
if(this.$parent[this.mpWeixinConfirm]){
// 执行父组件传递过来的方法
this.$emit("confirm",this.changeObj);
}
return false;
// #endif
// 判断父组件是否传递过来确认方法
if(this.$listeners['confirm']){
// 执行父组件传递过来的方法
this.$emit("confirm",this.changeObj);
}
},
// 执行组件取消按钮绑定方法
cancelFunction(){
// #ifdef MP-WEIXIN
// 判断父组件是否传递过来取消方法
// 次数区别处理,用以为 this.$listeners在微信小程序内获取为{}对象
if(this.$parent[this.mpWeixinCancel]){
// 执行父组件传递过来的方法
this.$emit("cancel");
}else{
// 执行默认方法
this.$refs.popup.close();
}
return false;
// #endif
// 判断父组件是否传递过来取消方法
if(this.$listeners['cancel']){
// 执行父组件传递过来的方法
this.$emit("cancel");
}else{
// 执行默认方法
this.$refs.popup.close();
}
},
}
}
</script>
<style>
.popupBox .dtitle{
position: relative;
}
.popupBox .dtitle .name{
text-align: center;
font-size: 36rpx;
font-weight: bold;
}
.popupBox .dtitle .close{
width: 54rpx;
height: 50rpx;
background: url("https://img.ssddjj.com/images/base/closeIcon.png") center center no-repeat;
background-size: 27rpx 25rpx;
position: absolute;
top: 0rpx;
right: 0rpx;
}
.popupBox .operate{
display: flex;
justify-content: space-evenly;
}
.popupBox .operate .btn{
width: 510rpx;
height: 80rpx;
line-height: 80rpx;
text-align: center;
color: white;
border-radius: 40rpx;
}
.popupBox .operate .btn.cancel{
background-color: #666666;
}
.popupBox .operate .btn.confirm{
background-color: #fed7d9;
}
.popupBox .operate .btn.confirm.cur{
background-color: #FA2228;
}
/* 拨盘公共样式 */
.picker-view {
/* width: 750rpx; */
height: 500rpx;
}
.picker-view .item {
height: 50px;
display: block;
line-height: 34px;
// #ifdef H5
line-height: 50px;
// #endif
align-items: center;
justify-content: center;
text-align: center;
}
// #ifdef H5
.uni-picker-view-indicator{
height: 50px;
line-height: 50px;
}
// #endif
/deep/.indicatorClassA{
height: 50px;
line-height: 50px;
border-top: 1rpx solid #FFA2A5;
border-bottom: 1rpx solid #FFA2A5;
}
/deep/.indicatorClassB{
height: 50px;
line-height: 50px;
}
</style>
在父页面使用:
页面展示
<view class="lis">
<view class="th">有效期</view>
<view class="lineBox">
<view class="lines">
<view class="row">
<view class="th" @click="openDatePicker(1)">起始时间</view>
<view class="uni-input" @click="openDatePicker(1)">{
{timeFilterStartTime?timeFilterStartTime:startInitDate}}</view>
</view>
</view>
</view>
</view>
<view class="lis">
<view class="th"></view>
<view class="lineBox">
<view class="lines">
<view class="row">
<view class="th" @click="openDatePicker(2)">结束时间</view>
<view class="uni-input" @click="openDatePicker(2)">{
{timeFilterEndTime?timeFilterEndTime:endInitDate}}</view>
</view>
</view>
</view>
</view>因为我是有一个开始时间和一个介绍时间 所以写了两遍组件
以下是弹窗打开的组件(记得import引入组件哦!!)
<!-- 日期时间拨盘选择组件 -->
<datePicker
title="起始时间"
type="yy-mm-dd hh:mm:ss"
minDate="2020-05-20 10:20:20"
maxDate="2025-05-20 10:20:50"
:initDate="startInitDate"
ref="startDatePicker"
@confirm="confirmFunction($event)"
mpWeixinConfirm="confirmFunction"
>
</datePicker>
<!-- 日期时间拨盘选择组件 -->
<datePicker
title="结束时间"
type="yy-mm-dd hh:mm:ss"
minDate="2020-05-20 10:20:20"
maxDate="2025-05-20 10:20:50"
:initDate="endInitDate"
ref="endDatePicker"
@confirm="confirmFunction($event)"
mpWeixinConfirm="confirmFunction"
>
</datePicker>data数据:
data() {
return {
timeFilterStartTime: '', //初始化筛选时间开始时间
startInitDate:'', //初始化默认开始时间
endInitDate:'', //初始化默认结束时间
timeFilterEndTime: '', //初始化筛选时间结束时间
open:'', //判断时间选择器类型
}
},这俩个字段是我要在页面展示的值
methods:
// 打开时间选择器
openDatePicker(type) {
this.open = type
if(this.open == 1){
this.$refs.startDatePicker.open();
}else{
this.$refs.endDatePicker.open();
}
},
// 时间选择器确认
confirmFunction($event){
if(this.open == 1){
this.timeFilterStartTime = $event[0].value+"-"+$event[1].value+"-"+$event[2].value+" "+$event[3].value+":"+$event[4].value+":"+$event[5].value;
}else{
this.timeFilterEndTime = $event[0].value+"-"+$event[1].value+"-"+$event[2].value+" "+$event[3].value+":"+$event[4].value+":"+$event[5].value;
}
},
// 获取初始开始时间
getStartInitDate(){
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth()+1;
var day = date.getDate();
var houer = date.getHours()
var minute = date.getMinutes()
var second = date.getSeconds();
this.startInitDate = year+"-"+(month>9?month:("0"+month))+"-"+(day>9?day:("0"+day))+" "+(houer?houer:("0"+houer))+":"+(minute?minute:("0"+minute))+":"+(second?second:("0"+second));
this.timeFilterStartTime = year+"-"+(month>9?month:("0"+month))+"-"+(day>9?day:("0"+day))+" "+(houer?houer:("0"+houer))+":"+(minute?minute:("0"+minute))+":"+(second?second:("0"+second));
},
// 获取初始结束时间
getEndInitDate(){
var date = new Date().getTime()+604800000;
date = new Date(date);
var year = date.getFullYear();
var month = date.getMonth()+1;
var day = date.getDate();
var houer = date.getHours()
var minute = date.getMinutes()
var second = date.getSeconds();
this.endInitDate = year+"-"+(month>9?month:("0"+month))+"-"+(day>9?day:("0"+day))+" "+(houer?houer:("0"+houer))+":"+(minute?minute:("0"+minute))+":"+(second?second:("0"+second));
this.timeFilterEndTime = year+"-"+(month>9?month:("0"+month))+"-"+(day>9?day:("0"+day))+" "+(houer?houer:("0"+houer))+":"+(minute?minute:("0"+minute))+":"+(second?second:("0"+second));
},
记得在页面加载时调用一下获取时间
onLoad() {
// 获取默认时间
this.getStartInitDate();
this.getEndInitDate();
},看着好乱 不过直接用应该没什么问题!
边栏推荐
猜你喜欢

【 LeetCode 】 235. A binary search tree in recent common ancestor

文本特征化方法总结

餐饮大单品「真香」,却没有穿透周期的能力

2022 Fusion Welding and Thermal Cutting Operation Certificate Exam Questions and Mock Exams

It turns out that Maya Arnold can also render high-quality works!Awesome Tips

本地能ping通虚拟机,虚拟机ping不通本地

TCP sticky packet unpacking problem + solution

TRACE32——List源代码查看

After working for 3 years, I recalled the comparison between the past and the present when I first started, and joked about my testing career

在anaconda Promat界面import torch通过,在jupyter notebook中报错的问题(仅提供思路理解!)
随机推荐
【win7】NtWaitForKeyedEvent
MAYA船的建模
[Shanghai] Hiring .Net Senior Software Engineer & BI Data Warehouse Engineer (Urgent)
文本特征化方法总结
Shiny04---Application of DT and progress bar in shiny
Summary of Text Characterization Methods
Vulnhub靶机:HA_ NARAK
After working for 3 years, I recalled the comparison between the past and the present when I first started, and joked about my testing career
In the anaconda Promat interface, import torch is passed, and the error is reported in the jupyter notebook (only provide ideas and understanding!)
餐饮大单品「真香」,却没有穿透周期的能力
Access Denied: "microsoft.web.ui.webcontrols" workaround
奇怪的Access错误
RK3568 environment installation
2022起重机司机(限桥式起重机)考试题库及模拟考试
Tencent Internship Summary
利用Jenkins的持续集成
Algorithm Supplements Fifteen Complementary Linked List Related Interview Questions
TRACE32——加载符号表信息用于调试
基于 Docker 快速使用远程(云)数据库
busybox 知:构建