根据具体时间转换为一周前、几小时前格式

2024-01-07 18:59:54
export function formatChangeTime(dateTimeStamp){
  // dateTimeStamp是一个时间毫秒,注意时间戳是秒的形式,在这个毫秒的基础上除以1000
 let minute = 1000 * 60;      //把分,时,天,周,半个月,一个月用毫秒表示
 let hour = minute * 60;
 let day = hour * 24;
 let week = day * 7;
 //var halfamonth = day * 15;
 let month = day * 30;

 let now = new Date().getTime();   //获取当前时间毫秒
 let diffValue = now - dateTimeStamp;//时间差

 if (diffValue < 0) { return; }

 let minC = diffValue / minute;  //计算时间差的分,时,天,周,月
 let hourC = diffValue / hour;
 let dayC = diffValue / day;
 let weekC = diffValue / week;
 let monthC = diffValue / month;
 let result
 if (monthC >= 1) {
     result = "" + parseInt(monthC) + "月前";
 }
 else if (weekC >= 1) {
     result = "" + parseInt(weekC) + "周前";
 }
 else if (dayC >= 1) {
     result = "" + parseInt(dayC) + "天前";
 }
 else if (hourC >= 1) {
     result = "" + parseInt(hourC) + "小时前";
 }
 else if (minC >= 1) {
     result = "" + parseInt(minC) + "分钟前";
 } else
     result = "刚刚";
 return result;
}

使用:

filters: {

? ? timerChange(time, that) {

? ? ? if (time) {

? ? ? ? // 转为时间戳

? ? ? ? let twmpTime = new Date(time).getTime()

? ? ? ? return that.formatChangeTime(twmpTime)

? ? ? }

? ? },

? },

<el-table-column

? ? ? ? prop="timeDifference"

? ? ? ? label="更新时间"

? ? ? ? width="70">

? ? ? ? <template slot-scope="scope">

? ? ? ? ? <div>

? ? ? ? ? ? {{ scope.row.timeDifference | timerChange(that) }}

? ? ? ? ? </div>

? ? ? ? </template>

? ? ? </el-table-column>

文章来源:https://blog.csdn.net/iseyre/article/details/134798497
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。