时间戳转年月日-基于vue

一般在src/utils里新建date.js

import Vue from 'vue';

// 时间戳转换为 YYYY-MM-DD HH:mm:ss
Vue.filter('formatDate', function(timeStamp, format) {
    if (timeStamp) {
      format = format || 'YYYY-MM-DD';
      let week = [
        '星期日',
        '星期一',
        '星期二',
        '星期三',
        '星期四',
        '星期五',
        '星期六'
      ];
      let date = new Date(parseInt(timeStamp));
      let o = {
        'M+': date.getMonth() + 1,
        'D+': date.getDate(),
        'h+': date.getHours() % 12,
        'H+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds(),
        'q+': Math.floor((date.getMonth() + 3) / 3),
        'S+': date.getMilliseconds(),
        'W+': week[date.getDay()]
      };
  
      if (/(Y+)/.test(format))
        format = format.replace(
          RegExp.$1,
          (date.getFullYear() + '').substr(4 - RegExp.$1.length)
        );
      for (let k in o)
        if (new RegExp('(' + k + ')').test(format))
          format = format.replace(
            RegExp.$1,
            RegExp.$1.length === 1
              ? o[k]
              : ('00' + o[k]).substr(('' + o[k]).length)
          );
      return format;
    }
  });

在需要的页面引入,

import { formatDate } from "@/utils/date.js";

在需要转码的地方:

{{ item.date | formatDate('YYYY-MM-DD HH:mm:ss')}}

 

posted @ 2020-07-14 20:02  Minoz_miao  阅读(1731)  评论(0编辑  收藏  举报