js实现时间显示的几分钟前
/**
* @param {number} time
* @returns {string}
*/
export function formatTime(time) {
const d = new Date(time).getTime()
const now = Date.now()
const diff = (now - d) / 1000
// console.log("🚀 ~ file: timeDate.js:10 ~ formatTime ~ diff:", diff)
if (diff < 30) {
return '刚刚'
} else if (diff < 3600) {
// less 1 hour
return Math.ceil(diff / 60) + '分钟前'
} else if (diff < 3600 * 24) {
return Math.ceil(diff / 3600) + '小时前'
} else if (diff < 3600 * 24 * 2) {
return '1天前'
} else if (diff >= 3600 * 24 * 2) {
return Math.ceil(diff / 3600 / 24) + '天前'
}
}