• art-template手动编写时间过滤器


    新版的art-template查看源码后,时间过滤器方面有问题,不能直接使用,所以这里我们手写一个过滤器到入口文件,这样就可以在其他地方直接使用

    (1)入口文件编写过滤方法

    /*引入模板引擎,注册一个过滤器 通过处理时间戳 转为日期格式(start)*/
    var template = require('art-template')
    /**
     * 时间戳格式化方法
     * @param date
     * @param format
     * @returns {void | string}
     */
    template.defaults.imports.dateFormat = function(date, format) {
        date = new Date(date);
        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;
    };
    /*注册一个过滤器 通过处理时间戳 转为日期格式(END)*/

      注意:首先需要下载好模板引擎

    (2)模板引擎调用

      

       结果如下所示:

      

    .

  • 相关阅读:
    9.8 查找表
    LeetCode——Permutations
    利用rman自己主动备份转储spfile
    linux下非root用户怎样改动root权限的文件
    做一个有主见的女生
    APP-FND-00676: 弹性域例程 FDFGDC 无法读取为此说明性弹性域指定的默认引用字段
    矩阵高速幂模板篇
    Index statistics collected bug
    位运算
    poj 1190 生日蛋糕 , 强剪枝
  • 原文地址:https://www.cnblogs.com/fightjianxian/p/12317246.html
Copyright © 2020-2023  润新知