zoukankan      html  css  js  c++  java
  • 时间戳转年月日-基于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')}}
  • 相关阅读:
    spring详解(五)——事物管理
    spring详解(四)——JDBC
    spring详解(三)——AOP
    Spring详解(二)
    Spring详解(一)
    springMVC(九)——restful风格和异常处理
    Linux 文件权限
    如何使用Git从GitHub上下载项目
    Spring(1)
    SpringMvc(4)
  • 原文地址:https://www.cnblogs.com/crystral/p/13301251.html
Copyright © 2011-2022 走看看