示例图
第一步,页面引用日期控件 步骤 (参考uniapp官方文档)
<template>
<view class="chooise-data">
<view class="yesterday" @click="prevDate()">
上一日
</view>
<view class="chooise-box">
<view class="date">
<view class="calendar">
<u-calendar v-model="show" ref="calendar" @change="change" :mode="mode" :btn-type="btnType"></u-calendar>
<view @click="showChange()">{{result}}</view>
</view>
<view class="date-icon">
<image style="width: 18px; height: 18px;" src="../static/img/home/pop-date.png" mode=""></image>
</view>
</view>
</view>
<view class="tomorrow" @click="nextDate">
下一日
</view>
</view>
</template>
javasScript 部分
<script>
export default {
data() {
return {
date: '',
show: false,
mode: 'date',
result: this.result,
btnType: 'primary',
};
},
beforeMount() {
this.result = this.getcurrentday()
},
methods: {
// 获取当前日期展示
getcurrentday() {
var date = new Date();
let year = date.getFullYear();
let month = date.getMonth() + 1;
let day = date.getDate();
month = month < 10 ? "0" + month : month; //月小于10,加0
day = day < 10 ? "0" + day : day; //day小于10,加0
return `${year}-${month}-${day}`;
},
// 获取时间戳日期的格式
// 因为不知道为什么,加了date初始化日期就显示为NAN,所以我写了两遍..o(╥﹏╥)o(可能是日期格式不对?)
getDate(date) {
var date = new Date(date);
let year = date.getFullYear();
let month = date.getMonth() + 1;
let day = date.getDate();
month = month < 10 ? "0" + month : month; //月小于10,加0
day = day < 10 ? "0" + day : day; //day小于10,加0
return `${year}-${month}-${day}`;
},
prevDate() {
//前一天
let odata = new Date(
//计算当前日期 -1
new Date(this.result).getTime() - 24 * 60 * 60 * 1000
);
this.result = this.getDate(odata); //格式化日期并赋值
},
nextDate() {
//后一天
let odata = new Date(
new Date(this.result).getTime() + 24 * 60 * 60 * 1000
); //计算当前日期 +1
this.result = this.getDate(odata); //格式化日期并赋值
},
showChange(index) {
this.show = true; //是否显示控件
},
change(e) {
console.log(e)
this.result = e.result; //当前日期显示到页面组件
}
},
watch: {
chooisedate(val) {
this.$emit('result', this.result) //子组件传值
}
},
}
</script>
Css部分(根据自己需求)
.chooise-data {
margin-top: 20px;
display: flex;
justify-content: space-between;
align-items: center;
width: 100%;
height: 40px;
}
.yesterday {
width: 80px;
height: 35px;
line-height: 35px;
text-align: center;
background-color: #2E95FF;
border: 1px solid #FFFFFF;
color: #FFFFFF;
border-radius: 5px;
}
.tomorrow {
width: 80px;
height: 35px;
line-height: 35px;
text-align: center;
background-color: #2E95FF;
border: 1px solid #FFFFFF;
color: #FFFFFF;
border-radius: 5px;
}