1.项目的坑,自己找的方案,并修改
2.代码
可以用到vue过滤或者方法里直接复制(记得去掉funcrion)
function timeago(dateTimeStamp) {
var arr = dateTimeStamp.split("T");
var d = arr[0];
var darr = d.split("-");
var t = arr[1];
var tarr = t.split(".000");
var marr = tarr[0].split(":");
var dd =
parseInt(darr[0]) +
"/" +
parseInt(darr[1]) +
"/" +
parseInt(darr[2]) +
" " +
parseInt(marr[0]) +
":" +
parseInt(marr[1]) +
":" +
parseInt(marr[2]);
//return this.formatDateTime(dd);
let time = new Date(Date.parse(dd));
time.setTime(time.setHours(time.getHours() + 8));
let Y = time.getFullYear();
let M =
time.getMonth() + 1 < 10
? "0" + (time.getMonth() + 1)
: time.getMonth() + 1;
let D = time.getDate() < 10 ? "0" + time.getDate() : time.getDate();
var h =
time.getHours() < 10 ? "0" + time.getHours() : time.getHours() + ":";
var m =
time.getMinutes() < 10
? "0" + time.getMinutes()
: time.getMinutes() + ":";
var s =
time.getSeconds() < 10 ? "0" + time.getSeconds() : time.getSeconds();
let data = Y + "/" + M + "/" + D + " " + h + m + s;
var minute = 1000 * 60; //把分,时,天,周,半个月,一个月用毫秒表示
var hour = minute * 60;
var day = hour * 24;
var week = day * 7;
var halfamonth = day * 15;
var month = day * 30;
console.log(new Date(data));
var now = new Date().getTime(); //获取当前时间毫秒
var diffValue = now - new Date(data).getTime(); //时间差
if (diffValue < 0) {
return;
}
var result = "";
var minute = 1000 * 60,
hour = minute * 60,
day = hour * 24,
halfamonth = day * 15,
month = day * 30,
year = month * 12,
_year = diffValue / year,
_month = diffValue / month,
_week = diffValue / (7 * day),
_day = diffValue / day,
_hour = diffValue / hour,
_min = diffValue / minute;
if (_year >= 1) result = parseInt(_year) + "年前";
else if (_month >= 1) result = parseInt(_month) + "个月前";
else if (_week >= 1) result = parseInt(_week) + "周前";
else if (_day >= 1) result = parseInt(_day) + "天前";
else if (_hour >= 1) result = parseInt(_hour) + "个小时前";
else if (_min >= 1) result = parseInt(_min) + "分钟前";
else result = "刚刚";
return result;
}