时间戳转年月日-基于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')}}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM