在全局配置main.js文件中:javascript
//時間戳轉化成格式時間 Vue.prototype.formatConversion = function (te){ if(te == ''){ return ''; }else if(te.length == 10){ var time = new Date(te * 1000); //時間戳爲10位需*1000,時間戳爲13位的話不需乘1000 var y = time.getFullYear(); var m = time.getMonth() < 9 ? '0' + (time.getMonth() + 1) : time.getMonth() + 1; var d = time.getDate() < 10 ? '0' + time.getDate() : time.getDate(); var h = time.getHours() < 10 ? '0' + time.getHours() : time.getHours(); var mm = time.getMinutes() < 10 ? '0' + time.getMinutes() : time.getMinutes(); var s = time.getSeconds() < 10 ? '0' + time.getSeconds() : time.getSeconds(); var timedate = y + '-' + m + '-' + d + ' ' + h + ':' + mm + ':' + s; return timedate; }else{ var time = new Date(te); var y = time.getFullYear(); var m = time.getMonth() < 9 ? '0' + (time.getMonth() + 1) : time.getMonth() + 1; var d = time.getDate() < 10 ? '0' + time.getDate() : time.getDate(); var h = time.getHours() < 10 ? '0' + time.getHours() : time.getHours(); var mm = time.getMinutes() < 10 ? '0' + time.getMinutes() : time.getMinutes(); var s = time.getSeconds() < 10 ? '0' + time.getSeconds() : time.getSeconds(); var timedate = y + '-' + m + '-' + d + ' ' + h + ':' + mm + ':' + s; return timedate; } }
在其餘文件中使用時:vue
import Vue from 'vue'; let vm = new Vue(); vm.formatConversion(1535623577);