<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>
【uview】picker 时间选择器 ,选择 前一天和后一天
最新推荐文章于 2024-03-18 15:23:43 发布