index.vue
<template>
<div>
<div>{{time | formatDate}}</div>
</div>
</template>
<script>
import { formatDate } from '../common/formatDate';
export default {
components: {},
props: {},
data() {
return {
time: 1469281964000,
times: '2019-04-16T12:24:48+08:00',
timess: '2021-07-21 09:54',
}
},
filters: {
formatDate(time) {
var date = new Date(time)
// return formatDate(date, 'YYYY-M-d h:m:s')
return formatDate(date, 'YYYY-MM-dd hh:mm:ss')
}
},
mounted() {},
methods: {}
}
</script>
<style scoped lang="scss">
</style>
formatDate.js
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);
}