时间计算规则
mixin.js####
export const TimeFiltering = {//时间格式化
created(){
Date.prototype.format = function(fmt) {
var o = {
"M+" : this.getMonth()+1, //月份
"d+" : this.getDate(), //日
"h+" : this.getHours(), //小时
"m+" : this.getMinutes(), //分
"s+" : this.getSeconds(), //秒
"q+" : Math.floor((this.getMonth()+3)/3), //季度
"S" : this.getMilliseconds() //毫秒
};
if(/(y+)/.test(fmt)) {
fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
}
for(var k in o) {
if(new RegExp("("+ k +")").test(fmt)){
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
}
}
return fmt;
}
}
}
.vue文件####
<template>
<div v-for="(item, index) in timeList" :key=\'index\'>{{jisuan(item.time)}}</div>
</template>
<script>
import {TimeFiltering} from \'../mixins/mixins\'
export default {
mixins: [TimeFiltering],
data(){
return {
timeList: [
{time: \'2018-12-01 20:24:14\'},
{time: \'2018-12-31 20:24:14\'},
{time: \'2019-1-01 20:24:14\'},
{time: \'2019-1-02 20:24:14\'},
{time: \'2019-1-03 20:24:14\'}
]
}
},
computed: {
jisuan(){
return function(time){
let nowTime = new Date().getTime()
let oldTime = new Date(time).getTime()
let oneDay = 24*60*60*1000
let twoDay = 2*24*60*60*1000
let oneWeek = 7*24*60*60*1000
let nowDay = new Date(time).getDay()
if (nowTime - oldTime < oneDay){
return new Date(time).format("hh:mm")
}else if( nowTime - oldTime < twoDay){
return \'昨天\'
} else if ( nowTime - oldTime < oneWeek ){
switch (nowDay) {
case 0:
return \'星期天\';
case 1:
return \'星期一\';
case 2:
return \'星期二\';
case 3:
return \'星期三\';
case 4:
return \'星期四\';
case 5:
return \'星期五\';
case 6:
return \'星期六\';
}
} else {
return new Date(time).format("yyyy-MM-dd hh:mm:ss")
}
}
}
}
}
</script>
版权声明:本文为yzyh原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。