vue时间戳转化

函数封装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// dateFilter.js
export function dateFilter (timestamp) {
function zero (time) {
return time < 10 ? '0' + time : time
}
var date = new Date(Number(timestamp) * 1000)// 时间戳为10位需*1000,时间戳为13位的话不需乘1000
let Y = date.getFullYear() + '-'
let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
let D = zero(date.getDate()) + ' '
let h = zero(date.getHours()) + ':'
let m = zero(date.getMinutes()) + ':'
let s = zero(date.getSeconds())
return Y + M + D + h + m + s
};

引入注册全局组件

1
2
3
4
5
6
// main.js
import Vue from 'vue'
// 引入时间戳过滤器
import { dateFilter } from './filter/datefilter'
// 注册为全局过滤器
Vue.filter('dateFilter', dateFilter)

使用

1
2
3
<template>
<div class="time">{{ data.time | dateFilter }}</div>
</template>

 

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×