// 多少天前,分鐘markdown
computeTime: function (startTime, endTime) {
let date3 = new Date(endTime).getTime() - new Date(startTime).getTime(); //時間差的毫秒數
//計算出相差天數
let days = Math.floor(date3 / (24 * 3600 * 1000))
//計算出小時數
let leave1 = date3 % (24 * 3600 * 1000) //計算天數後剩餘的毫秒數
let hours = Math.floor(leave1 / (3600 * 1000))
//計算相差分鐘數
let leave2 = leave1 % (3600 * 1000) //計算小時數後剩餘的毫秒數
let minutes = Math.floor(leave2 / (60 * 1000))
//計算相差秒數
let leave3 = leave2 % (60 * 1000) //計算分鐘數後剩餘的毫秒數
let seconds = Math.round(leave3 / 1000)
console.log(" 相差 " + days + "天 " + hours + "小時 " + minutes + " 分鐘" + seconds + " 秒")
},
複製代碼
function jsDateDiff(pTime){
var d_minutes,d_hours,d_days,d;
var timeNow = parseInt(new Date().getTime()/1000);
pTime_new = new Date(pTime).getTime()/1000;
d = timeNow - pTime_new;
d_days = parseInt(d/86400);
d_hours = parseInt(d/3600);
d_minutes = parseInt(d/60);
if(d_days>0 && d_days<4){
return d_days+"天前";
}else if(d_days<=0 && d_hours>0){
return d_hours+"小時前";
}else if(d_hours<=0 && d_minutes>0){
return d_minutes+"分鐘前";
}else{
return pTime;
}
}
alert(jsDateDiff('2018/12/17 22:00:00'));
複製代碼
// utc計算朋友圈時間spa
function getDateDiff(dateStr) {
// var publishTime = getDateTimeStamp(dateStr) / 1000,
var publishTime = new Date(dateStr).getTime() / 1000, // 時間戳
d_seconds,
d_minutes,
d_hours,
d_days,
timeNow = parseInt(new Date().getTime() / 1000),
d,
date = new Date(publishTime * 1000),
Y = date.getFullYear(),
M = date.getMonth() + 1,
D = date.getDate(),
H = date.getHours(),
m = date.getMinutes(),
s = date.getSeconds();
//小於10的在前面補0
if (M < 10) {
M = '0' + M;
}
if (D < 10) {
D = '0' + D;
}
if (H < 10) {
H = '0' + H;
}
if (m < 10) {
m = '0' + m;
}
if (s < 10) {
s = '0' + s;
}
d = timeNow - publishTime;
d_days = parseInt(d / 86400);
d_hours = parseInt(d / 3600);
d_minutes = parseInt(d / 60);
d_seconds = parseInt(d);
if (d_days > 0 && d_days < 3) {
return d_days + '天前';
} else if (d_days <= 0 && d_hours > 0) {
return d_hours + '小時前';
} else if (d_hours <= 0 && d_minutes > 0) {
return d_minutes + '分鐘前';
} else if (d_seconds < 60) {
if (d_seconds <= 0) {
return '剛剛';
} else {
return d_seconds + '秒前';
}
} else if (d_days >= 3 && d_days < 30) {
return M + '.' + D + ' ' + H + ':' + m;
} else if (d_days >= 30) {
return Y + '.' + M + '.' + D + ' ' + H + ':' + m;
}
}複製代碼