实例方法

1
2
3
4
5
6
7
8
9
10
11
12
13
// 时间戳过滤
Vue.filter('moment'(timestamp, pattern = 'YYYY-MM-DD HH:mm:ss') => {
if (!timestamp) return new Error('时间戳为空!')
if (['number', 'string'].indexOf(timestamp) === -1) return new Error('时间戳格式有误!')
    const MS = 13
    let dataStr, dataLength
    if (typeof timestamp === "number") {
        dataStr = timestamp.toString()
    }
    dataLength = dataStr.length
    timestamp = dataLength > MS ? Math.floor(timestamp / (dataLength - MS)) : timestamp * (10 ** (MS - dataLength))
    return moment(timestamp).format(pattern)
})