Files
learning-system-portal/src/components/Portal/datetimeShow.vue
2022-05-29 18:56:34 +08:00

73 lines
2.0 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<!--此组件用于显示时间几天前几个小时前-->
<span>{{showTime}}</span>
</template>
<script>
export default {
props:{
css:{
type:String,
default:''
},
time:{
type:String,
default:''
}
},
data(){
return {
}
},
computed:{
showTime(){
if(this.time!=null && this.time!=''){
let showStr='';
let dt1=new Date(this.time.replace(/-/g,'/'));
let dt2=new Date();
var dateDiff = dt2.getTime() - dt1.getTime();//时间差的毫秒数
var dayDiff = Math.floor(dateDiff / (24 * 3600 * 1000));//计算出相差天数
var leave1=dateDiff%(24*3600*1000) //计算天数后剩余的毫秒数
var hours=Math.floor(leave1/(3600*1000))//计算出小时数
var leave2=leave1%(3600*1000) //计算小时数后剩余的毫秒数
var minutes=Math.floor(leave2/(60*1000))//计算相差分钟数
var leave3=leave2%(60*1000) //计算分钟数后剩余的毫秒数
var seconds=Math.round(leave3/1000)
if(dayDiff>31){
let monthDiff=Math.floor(dayDiff/30);//以30天为一个月不够精准严谨
if(monthDiff>12){
let yearDiff=Math.floor(monthDiff/12);//获取相差的年份
// showStr=yearDiff+'年之前';
showStr = this.time;
}else{
// showStr=monthDiff+'个月之前';
showStr = this.time;
}
}else{
if(dayDiff>0){
// showStr=dayDiff+'天之前';
showStr = this.time;
}else if(hours>0){
showStr=hours+'小时之前';
}else if(minutes>0){
showStr=minutes+'分钟之前';
}else if(seconds>0){
showStr=seconds+'秒之前';
}else{
showStr='刚刚';
}
}
return showStr;
}else{
return '';
}
}
}
}
</script>
<style>
</style>