vue+elementUI实现日期选择器的上一日、下一日按钮

el-date-picker中我想额外实现点击上一日按钮、下一日按钮显示对应的日期。这里面需要借助时间戳转换(先转换成时间戳,再讲时间戳转化成具体时间,然后截取日期)。

效果

代码

template:

              <el-form :inline="true" :model="dataForm" @submit.native.prevent>
                <el-form-item label="日期:">
                  <el-date-picker
                    v-model="selectDate2"
                    type="date"
                    size="small"
                    width="120"
                    placeholder="选择日期"
                  >
                  </el-date-picker>
                </el-form-item>
                <el-form-item>
                  <el-button size="small" type="primary" @click="goLast(1)"
                    >上一日</el-button
                  >
                </el-form-item>
                <el-form-item>
                  <el-button size="small" type="primary" @click="goNextDay(1)">下一日</el-button>
                </el-form-item>
                <el-form-item> (历史数据) </el-form-item>
              </el-form>

js:

export default {
  data() {
    return {
      selectDate: "",
      selectDate2: ""
    }
   },

  methods: {
    //将10位或者13位时间戳转化为日期格式
    timestampToTime(timestamp) {
      if (String(timestamp).length == 10) {
        var unixtimestamp = new Date(timestamp * 1000);
        var year = 1900 + unixtimestamp.getYear();
        var month = "0" + (unixtimestamp.getMonth() + 1);
        var date = "0" + unixtimestamp.getDate();
        var hour = "0" + unixtimestamp.getHours();
        var minute = "0" + unixtimestamp.getMinutes();
        var second = "0" + unixtimestamp.getSeconds();
        return (
          year +
          "-" +
          month.substring(month.length - 2, month.length) +
          "-" +
          date.substring(date.length - 2, date.length) +
          " " +
          hour.substring(hour.length - 2, hour.length) +
          ":" +
          minute.substring(minute.length - 2, minute.length) +
          ":" +
          second.substring(second.length - 2, second.length)
        );
      } else {
        let currentTime = new Date(timestamp);
        let year = currentTime.getFullYear(),
          month = currentTime.getMonth() + 1,
          day = currentTime.getDate(),
          hour = currentTime.getHours(),
          minute = currentTime.getMinutes(),
          second = currentTime.getSeconds(),
          millSeconds = currentTime.getMilliseconds();
        let months = month < 10 ? "0" + month : month,
          days = day < 10 ? "0" + day : day,
          hours = hour < 10 ? "0" + hour : hour,
          minutes = minute < 10 ? "0" + minute : minute,
          seconds = second < 10 ? "0" + second : second,
          millSecondss =
            millSeconds < 10
              ? "00" + millSeconds
              : millSeconds < 100
              ? "0" + millSeconds
              : millSeconds;
        return (
          year +
          "-" +
          months +
          "-" +
          days +
          " " +
          hours +
          ":" +
          minutes +
          ":" +
          seconds +
          "." +
          millSecondss
        );
      }
    },
        //显示当前时间
        getNowTime() {
            let now = new Date(),
                year = now.getFullYear(), //得到年份
                month = now.getMonth(), //得到月份
                date = now.getDate(); //得到日期
                 console.log(date);
            let time = year + "-" + month + "-" + date;
            this.selectDate = time;
            this.selectDate2 = time;
        },
    //上一日
    goLast(index) {
      if (index === 0) {
        let date1 = new Date(this.selectDate).getTime() - 3600 * 1000 * 24;
        this.selectDate = this.timestampToTime(date1).substring(0,10);
      } else if (index === 1) {
         let date2 = new Date(this.selectDate2).getTime() - 3600 * 1000 * 24;
        this.selectDate2 = this.timestampToTime(date2).substring(0,10);
      }
    },

    //下一日
    goNextDay(index) {
      if (index === 0) {
        let date3 = new Date(this.selectDate).getTime() + 3600 * 1000 * 24;
        this.selectDate = this.timestampToTime(date3).substring(0,10);
      } else if (index === 1) {
        let date4 = new Date(this.selectDate2).getTime() + 3600 * 1000 * 24;
        this.selectDate2 = this.timestampToTime(date4).substring(0,10);
      }
    }

  },
  created() {
      this.getNowTime();
  }
}

总结:

1.后面substring(0,10)是因为转换后的时间是精确到毫秒级的(13位时间戳)或者精确到秒级(10位时间戳)。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尔嵘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值