记录工作遇到时间的各种处理

475 阅读1分钟

1.vant中倒计时处理

效果 在这里插入图片描述

由于vant返回的就是HH: mm : ss 格式,所以切割就可以了,如果返回的是其他格式例如2021-4-17之类的,就得先加入new Date()中,再分别取出年月日切割.

        <!-- 引入倒计时 -->
        <van-count-down :time="time" format="HH: mm : ss ">
          <template #default="timeData">
            <span class="countDownBlock">{{
              timeData.hours | dateFormat("1")
            }}</span>
            <span class="countDownBlock">{{
              timeData.hours | dateFormat("2")
            }}</span>
            <span class="countDownColon">:</span>
            <span class="countDownBlock">{{
              timeData.minutes | dateFormat("1")
            }}</span>
            <span class="countDownBlock">{{
              timeData.minutes | dateFormat("2")
            }}</span>
            <span class="countDownColon">:</span>
            <span class="countDownBlock">{{
              timeData.seconds | dateFormat("1")
            }}</span>
            <span class="countDownBlock">{{
              timeData.seconds | dateFormat("2")
            }}</span>
          </template>
        </van-count-down>
 filters: {
    // 过滤成单个时间,如果不满10,就加上0
    dateFormat(value, type) {
      let data = value.toString();
      data = data < 10 ? "0" + data : data;
      if (type == 1) {
        let filterDate = data.slice(0, 1);
        return filterDate;
      } else {
        let filterDate = data.slice(1, 2);
        return filterDate;
      }
    },

2.格式化日期yyyy-MM

这里获得的格式就是Sat Apr 17 2021 17:12:46 GMT+0800 (中国标准时间)类型,如果不是,如上所述,需要先转化,例如:new Date(date)

  formatTime(date) {
      var YY = date.getFullYear();
      var MM =
        date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1;
      return YY + "-" + MM;
    },

3.yyyy MM转为yyyy年MM月

spe切割的是标点符号,比如2021-04中的"-",2021.04中的"."

  selectTime(value, spe) {
      let date = value.split(spe);
      return `${date[0]}${date[1]}月`;
    },

4.日期相加或相减获取截止时间

这里是传入年月日,返回数组形式的年月日,可以根据业务做调整

    gettime(vyear, vmonth, vday) {
      var date = new Date();
      var nyear = date.getFullYear() + vyear; //获取年份差
      var nmonth = date.getMonth() + vmonth; //获取月份差
      var nday = date.getDate() + vday; //获取日差
      var ux = Date.UTC(nyear, nmonth, nday); //转化为时间戳
      var date1 = new Date(ux); //构造目标日期
      return [date1.getFullYear(), date1.getMonth() + 1, date1.getDate()];
    },

5.格式化时间yyyy-MM-dd hh:MM:ss

formatDate(date) {
      var date = new Date(date);
      var YY = date.getFullYear() + "-";
      var MM =
        (date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1) + "-";
      var DD = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
      var hh =
        (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
      var mm =
        (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) +
        ":";
      var ss =
        date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
      return YY + MM + DD + " " + hh + mm + ss;
    },