vue 按钮点击前一天,后一天改变日期
程序员文章站
2022-07-02 20:22:59
...
<template>
<div>{{dateTime}}</div>
<div>
<button @click="prevDate">前一天</button>
<button @click="nextDate">后一天</button>
</div>
</template>
<script>
data() {
return {
selDateTimes:"2020-01-21"
},
methods: {
prevDate() {
//前一天
let odata = new Date(
new Date(this.selDateTimes).getTime() - 24 * 60 * 60 * 1000
); //计算当前日期 -1
this.selDateTimes = this.convertToDate(odata); //格式化日期并赋值
},
nextDate() {
//后一天
let odata = new Date(
new Date(this.selDateTimes).getTime() + 24 * 60 * 60 * 1000
); //计算当前日期 +1
this.selDateTimes = this.convertToDate(odata); //格式化日期并赋值
},
convertToDate(date) {
var date = new Date(date);
var y = date.getFullYear();
var m = date.getMonth() + 1;
var d = date.getDate();
m = m < 10 ? "0" + m : m; //月小于10,加0
d = d < 10 ? "0" + d : d; //day小于10,加0
return y + "-" + m + "-" + d;
}
}
}
</script>
推荐阅读