JS,快应用,前后日期点击组件

4 篇文章 0 订阅
2 篇文章 0 订阅

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>

实现效果:
在这里插入图片描述默认当天时间,左右点击切换时间

前端菜鸡,各位大佬别喷~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值