<template>
<view>
<button type="" @click="last()">上一月</button>
<button type="" @click="next()">下一月</button>
<view class="" >
{{startDate}}
</view>
<view class="">
{{endDate}}
</view>
</view>
</template>
<script>
export default {
data() {
return {
nowDate:"",
fullYear:"",
month:"",
endOfMonth:"",
endDate:"",
startDate:""
}
},
onReady() {
this.nowDate = new Date();
this.fullYear = this.nowDate.getFullYear();
this.month = this.nowDate.getMonth() + 1; // getMonth 方法返回 0-11,代表1-12月
this.endOfMonth = new Date(this.fullYear, this.month, 0).getDate(); // 获取本月最后一天
this.endDate = this.getFullDate(this.nowDate.setDate(this.endOfMonth));//当月最后一天
this.startDate = this.getFullDate(this.nowDate.setDate(1));//当月第一天
},
methods: {
getFullDate(targetDate) {
var D, y, m, d;
if (targetDate) {
D = new Date(targetDate);
y = D.getFullYear();
m = D.getMonth() + 1;
d = D.getDate();
} else {
y = fullYear;
m = month;
d = date;
}
m = m > 9 ? m : '0' + m;
d = d > 9 ? d : '0' + d;
return y + '-' + m + '-' + d;
},
last(){
this.nowDate.setMonth(this.nowDate.getMonth()-1);
this.startDate = this.nowDate.getFullYear() + "-" + (this.nowDate.getMonth()+1) + "-" + this.nowDate.getDate()
this.endDate =this.getFullDate(new Date(this.nowDate.getFullYear(), this.nowDate.getMonth() + 1, 0))
},
next(){
this.nowDate.setMonth(this.nowDate.getMonth()+1);
this.startDate = this.nowDate.getFullYear() + "-" + (this.nowDate.getMonth()+1) + "-" + this.nowDate.getDate()
this.endDate =this.getFullDate(new Date(this.nowDate.getFullYear(), this.nowDate.getMonth() + 1, 0))
}
},
}
</script>
<style>
</style>
uniapp获取上个月、下个月的月初和月末的日期实现
原创
©著作权归作者所有:来自51CTO博客作者一粒程序米的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章