vue时间格式化
2017-04-21 00:12
429 查看
export function formatDate(date, fmt) { if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length)); } let o = { 'M+': date.getMonth() + 1, 'd+': date.getDate(), 'h+': date.getHours(), 'm+': date.getMinutes(), 's+': date.getSeconds() }; for (let k in o) { if (new RegExp(`(${k})`).test(fmt)) { let str = o[k] + ''; fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str)); } } return fmt; }; function padLeftZero(str) { return ('00' + str).substr(str.length); }
把上面代码保存为date.js放到你的公共js文件夹中。
在你的需要格式化时间戳的组件里像下面这样使用:
<template> <!-- 过滤器 --> <div>{{time | formatDate}}</div> <!-- 输出结果 --> <!-- <div>2016-07-23 21:52</div> --> </template> <script> import {formatDate} from './common/date.js'; export default { data() { return { time:1469281964000 } }, filters: { formatDate(time) { var date = new Date(time); return formatDate(date, "yyyy-MM-dd hh:mm"); } } } </script>
相关文章推荐
- vue.js格式化时间并每秒更新
- vue2.0的Element UI的表格table列时间戳格式化
- vue 时间格式化
- vue时间格式化实例代码
- vue2.0的Element UI的表格table列时间戳格式化
- vue2.0的Element UI的表格table列时间戳格式化
- vue.js实现格式化时间并每秒更新显示功能示例
- vue2.0的Element UI的表格table列时间戳格式化
- vue 时间格式化
- vue-格式化时间戳+格式化编程思想
- vue2.0的Element UI的表格table列时间戳格式化
- Vue filter格式化时间戳时间成标准日期格式的方法
- vue时间格式化
- Vue filter方法 ‘ | ’(通道)————时间格式化
- ORACLE——日期时间格式化参数详解 之一
- c#时间格式化字符串详解(转)
- ios--时间格式化--NSDateFormatter
- Java中的时间格式化和时间计算的方法
- 时间格式化大全
- 分享段代码-格式化时间(显示秒值为**年**月**天 **时**分**秒 如1年2个月3天 10小时)