美文网首页
Vue手写时间过滤器

Vue手写时间过滤器

作者: 菜的只能打代码 | 来源:发表于2020-11-06 15:23 被阅读0次

使用方法: <td  v-cloak>{{item.date | format('yyyy-MM-dd hh:mm:ss')}}</td> 


定义:

  Vue.filter('format', function(value, arg) {

      function dateFormat(date, format) {

        if (typeof date === "string") {

          var mts = date.match(/(\/Date\((\d+)\)\/)/);

          if (mts && mts.length >= 3) {

            date = parseInt(mts[2]);

          }

        }

        date = new Date(date);

        if (!date || date.toUTCString() == "Invalid Date") {

          return "";

        }

        var map = {

          "M": date.getMonth() + 1, //月份 

          "d": date.getDate(), //日 

          "h": date.getHours(), //小时 

          "m": date.getMinutes(), //分 

          "s": date.getSeconds(), //秒 

          "q": Math.floor((date.getMonth() + 3) / 3), //季度 

          "S": date.getMilliseconds() //毫秒 

        };

        format = format.replace(/([yMdhmsqS])+/g, function(all, t) {

          var v = map[t];

          if (v !== undefined) {

            if (all.length > 1) {

              v = '0' + v;

              v = v.substr(v.length - 2);

            }

            return v;

          } else if (t === 'y') {

            return (date.getFullYear() + '').substr(4 - all.length);

          }

          return all;

        });

        return format;

      }

      return dateFormat(value, arg);

    })

相关文章

  • Vue手写时间过滤器

    使用方法: {{item.date|format('yyyy-MM-ddhh:mm:ss')}} 定义: Vue...

  • tool.js

    vue时间格式过滤器(今天,昨天,周几,年月日) vue金额过滤器 手机横屏签名功能(vue)

  • vue filter 过滤器使用

    格式化时间 自定义全局过滤器 vue 自定义过滤器分为"全局过滤器"和"局部过滤器"两种。 一、 全局过滤器 全局...

  • 2018-09-17

    1.vue 过滤器的用法 生成的效果 48 2.vue 用过滤器实现时间 生成的效果 2018年9月17日,星...

  • 13、vue 中格式化时间

    过滤器格式化时间 date.js 格式化方法使用 vue中过滤器使用 v-model中格式化时间(过滤器就失效了)...

  • 6.Vue过滤器

    Vue过滤器: vue过滤器使用管道 | 进行调用,如:{{name | myFilter}},如果需要传入参数...

  • Vue-04

    过滤器:对显示在页面上的数据进行筛选 全局过滤器 和Vue同级 Vue.filter(“过滤器名称”,func...

  • vue自定义过滤器

    Vue的自定义过滤器有两种:全局过滤器和内部过滤器全局过滤器定义在vue实例化之前 内部过滤器注册在实例内部,仅在...

  • 9.自定义vue全局过滤器

    1.Vue.filter('过滤器名字',过滤器函数):

  • vue 过滤器做字数限制并显示省略号

    定义过滤器 使用vue中的 过滤器filters

网友评论

      本文标题:Vue手写时间过滤器

      本文链接:https://www.haomeiwen.com/subject/nhmtbktx.html