utils文件下
// 时间格式转化
// time: 时间戳或者时间对象 type:转换格式
const timeFormat = (time, type) => {
var date;
if (time instanceof Date) { //判断传入的是时间戳还是时间对象
date = time;
} else {
date = new Date(time)
}
var y = date.getFullYear();
var m = date.getMonth() + 1;
m = m < 10 ? "0" + m : m;
var d = date.getDate();
d = d < 10 ? "0" + d : d;
var h = date.getHours();
h = h < 10 ? "0" + h : h;
var minute = date.getMinutes();
minute = minute < 10 ? "0" + minute : minute;
var second = date.getSeconds();
second = second < 10 ? "0" + second : second;
switch (type) {
case 'yy-mm-dd hh:mm:ss':
return y + "-" + m + "-" + d + " " + h + ":" + minute + ":" + second;
case 'yy-mm-dd type':
return y + "-" + m + "-" + d + " " + (h >= 13 ? '下午' : '上午');
case 'yy-mm-dd hh:mm':
return y + "-" + m + "-" + d + " " + h + ":" + minute;
case 'yy/mm/dd hh:mm:ss':
return y + "/" + m + "/" + d + " " + h + ":" + minute + ":" + second;
case 'yy-mm-dd':
return y + "-" + m + "-" + d;
case 'yy.mm.dd':
return y + "." + m + "." + d;
case 'yy.mm.dd hh:mm:ss':
return y + "." + m + "." + d + " " + h + ":" + minute + ":" + second;
case 'mm-dd hh:mm':
return m + "-" + d + " " + h + ":" + minute;
}
}
export { timeFormat }
main.js
import * as filters from '../utils/filters'
//全局注册过滤器
console.log(filters);
Object.keys(filters).forEach((key) => {
console.log(key);
console.log(filters[key]);
Vue.filter(key, filters[key])
}
);
vue单文件
<template>
<div>
<h3>{{ time | timeFormat("yy-mm-dd hh:mm:ss") }}</h3>
</div>
</template>
<script>
export default {
name: "",
data() {
return {
time: new Date(), //获取当前时间戳
};
},
};
</script>
<style scoped>
</style>