自定义全局时间过滤器
运行:
cnpm i moment -S
(不推荐使用,因为代码体积太大,不如自己写呢!!!)
- 方法1:引入 moment 时间处理格式的安装包
定义全局过滤器,需要在
main.js
中把过滤器函数挂载给React.Component.prototype
// 模拟全局过滤器 import moment from 'moment' React.Component.prototype.dateFormat = function (dt) { return moment(dt).format('YYYY-MM-DD HH:mm:ss') }
- 方法2:自己封装时间过滤器
React.Component.prototype.dateFormat = function (dt) { const dt = new Date(dt) const y = dt.getFullYear() const m = (dt.getMonth + 1).toString().padStart(2,'0') const d = dt.getDate().toString().padStart(2,'0') const hh = dt.getHours().toString().padStart(2,'0') const mm = dt.getMinutes().toString().padStart(2,'0') const ss = dt.getSeconds().toString().padStart(2,'0') return `${y}-${m}-${d} ${hh}:${mm}:${ss}` }