JS快应用左右日期点击组件:
<template>
<div class="News-container">
<div style="margin: 0 auto;">
<image class="arrow" @click="reduce()" src="../img/zuo.png" style="margin-right: 10px;"></image>
<text class="yangli_text">{{isDate}}</text>
<image class="arrow" @click="plus()" src="../img/you.png" style="margin-left: 10px;"></image>
</div>
</div>
</template>
<style>
.News-container {
width: 100%;
height: 100%;
background-color: #fff;
}
.arrow {
width: 40px;
height: 40px;
margin-top: 5px;
}
.yangli_text {
text-align: center;
font-size: 26px;
color: #c30f22;
}
</style>
<script>
let date = new Date()
let year = date.getFullYear()
let month = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1)
let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
module.exports = {
data: {
isDate: '',
},
onInit: function () {
this.isDate = year + '-' + month + '-' + day
},
//倒退时间
reduce() {
if (day == 1 && month == 1) {
day = new Date(year, month, 0).getDate()
month = 12
year = (year - 1)
this.isDate = year + '-' + month + '-' + day
} else if (day == 1) {
day = new Date(year, (month - 1), 0).getDate()
month = (month - 1)
if (month < 10) {
month = '0' + Number(month)
}
this.isDate = year + '-' + month + '-' + day
} else {
day = Number(day) - 1
if (day < 10) {
day = '0' + Number(day)
}
if (month < 10) {
month = '0' + Number(month)
}
this.isDate = year + '-' + month + '-' + day
}
this.$dispatch('reduceClick', { isDate: this.isDate })
},
//前进时间
plus() {
console.log(new Date(year, month, 0).getDate());
if (day === new Date(year, month, 0).getDate() && month === 12) {
day = 1
month = 1
year = Number(year) + 1
this.isDate = year + '-' + (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day)
} else if (day === new Date(year, month, 0).getDate()) {
day = 1
month = Number(month) + 1
this.isDate = year + '-' + (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day)
} else {
day = Number(day) + 1
month = Number(month)
this.isDate = year + '-' + (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day)
}
this.$dispatch('plusClick', { isDate: this.isDate })
},
}
</script>
实现效果: 默认当天时间,左右点击切换时间
前端菜鸡,各位大佬别喷~
|