【uview】picker 时间选择器 ,选择 前一天和后一天

在这里插入图片描述在这里插入图片描述

<template>
 <view>
  <view class="topview flex_spacebetween">
   <view class="leftview flex_align_center" @click="frontFun">
    <image src="@/pagesA/static/images/icon_left.png" mode=""></image>
    <text class="p_l_6">前一天</text>
   </view>
   <view class="flex_align_center pickerview" >
		 <picker mode="date" :value="date"  @change="bindDateChange">
				 <text>{{year}}</text><text>{{month}}</text><text>{{day}}</text></picker>
   </view>
   <view class="rightview flex_align_center" @click="afterFun">
		<text>后一天</text>
    <image src="@/pagesA/static/images/icon_right.png" mode="" class="p_l_6"></image>
   </view>
  </view>

 
 </view>
</template>

<script>
	function getDate(type) {
	    const date = new Date();
	    let year = date.getFullYear();
	    let month = date.getMonth() + 1;
	    let day = date.getDate();
			
	    month = month > 9 ? month : '0' + month;
	    day = day > 9 ? day : '0' + day;
	    return `${year}-${month}-${day}`;
		
	}
 // import {
 //   xxx
 //  } from "@/api/index/user.js"
 export default {
  components: {
  
  },
  data() {
   return {
		date: getDate({
				format: true
		}),
		 // ======================
			// maxDate: null, // 限制最大时间,当前时间后一天
			year:'',
			month:'',
			day:'',
			timeStamp:'', // 时间戳
   }
  },
  onLoad(option) {
   this.gettime()
  },

  methods: {
			// 获取当前时间和时间戳
			gettime(){
				let time = new Date();
				let a = time.getFullYear();
				let b = time.getMonth() + 1;
				let c = time.getDate();
				a = a<10?'0'+a : a;
				b = b<10?'0'+b : b;
				c = c<10?'0'+c : c;
				this.year = a;
				this.month = b;
				this.day = c;
				this.timeStamp = new Date().getTime();
				// this.maxDate = new Date().getTime() +  24 * 60 * 60 * 1000; 
			},
			// 时间与时间戳 同时赋值
			settime(val){
				let date = new Date(val);
				let y = date.getFullYear();
				let MM = date.getMonth() + 1;
				let d = date.getDate();
				MM = MM < 10 ? ('0' + MM) : MM;
				d = d < 10 ? ('0' + d) : d;
				
				this.year = y;
				this.month = MM;
				this.day = d;
				this.timeStamp = val;
				this.date = `${this.year}-${this.month}-${this.day}`
			},
		// 前一天
		frontFun(){
			let a = this.timeStamp -  24 * 60 * 60 * 1000
			this.settime(a)
		},
		afterFun(){
			let b = this.timeStamp + 24 * 60 * 60 * 1000
			this.settime(b)
			
			// if(b <= this.maxDate){
			// 	this.settime(b)
			// }else{
			// 	uni.showToast({
			// 		title:'只支持查看当前时间后一天的数据',
			// 		icon:'none'
			// 	})
			// 	return;
			// }
		},
		
		 bindDateChange: function(e) {
				this.date = e.detail.value; // 年月日
				const val = Date.parse(new Date(e.detail.value));
				this.settime(val)
		}
		
	
	
  },
 }
</script>

<style lang='scss' scoped>
 .content{
  padding:0 30rpx 30rpx;
 }
 .topview{
  image{
   width: 32rpx;
   height: 32rpx;
  }
	.pickerview{
		text{
			color: #2770FF;
			font-size: 32rpx;
			padding: 0 10rpx;
		}
	}
	.leftview,.rightview{
		font-size: 28rpx;
		color: #333333;
	}
 }

</style>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值