• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Vue时间戳转时间

武飞扬头像
骨子里的偏爱
帮助1

Vue时间戳转时间

一、效果展示
学新通
二、步骤如下

1.在common/utils.js下写入转换条件
学新通

// 时间戳转换
export function formatDate(date, fmt) {
    //1、获取年份
    if (/(y )/.test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (date.getFullYear()   '').substr(4 - RegExp.$1.length));
    }
    //2、获取
    let o = {
      'M ': date.getMonth()   1,
      'd ': date.getDate(),
      'h ': date.getHours(),
      'm ': date.getMinutes(),
      's ': date.getSeconds()
    };
    for (let k in o) {
      if (new RegExp(`(${k})`).test(fmt)) {
        let str = o[k]   '';
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
      }
    }
    return fmt;
  }
  
  function padLeftZero (str) {
    return ('00'   str).substr(str.length);
  }
  
学新通

2.在此处引入,并传入样式
学新通

<script>
  import {formatDate} from "common/utils";
  export default {
    name: "DetailCommentInfo",
    props: {
      commentInfo: {
        type: Object,
        default() {
          return {}
        }
      }
    },
    filters: {
      showDate: function (value) {
        let date = new Date(value * 1000);
        return formatDate(date, 'yyyy-MM-dd hh:mm:ss');
      }
    }
  }
</script>
学新通

获取的数据,转换并显示

     <div class="info-other">
        <span class="date">{{commentInfo.created | showDate}}</span>
        <span>{{commentInfo.style}}</span>
      </div>

css样式:

.info-detail .info-other {
    font-size: 12px;
    color: #999;
    margin-top: 10px;
  }
  .info-other .date {
    margin-right: 8px;
  }

如何通过父传子的方法传递给孩子数据:
学新通
获取请求的数据:
学新通
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhghacei
系列文章
更多 icon
同类精品
更多 icon
继续加载