uniapp获取上个月、下个月的月初和月末的日期实现_日期

uniapp获取上个月、下个月的月初和月末的日期实现_日期_02

uniapp获取上个月、下个月的月初和月末的日期实现_日期_03

<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>