由于业务需求要做一个评论功能,评论的时间要求是多久前。
评论列表友好显示时间,显示多久时间前评论
思路:
- 获取后台返回的时间戳与本地时间戳的差值,差值就是多久前评论的时间
- 通过比较差值与60秒,1小时,2天大小,判断是多少秒前,多少分钟前,昨天,前天,或具体日期具体可看代码
代码已经封装,可以放到独立的js文件作为单独的工具函数使用
/* eslint-disable */
// 分,时,天,转秒
const DATE_CONFIG = {
m: 60,
h: 3600,
d: 86400,
daysOfYeay: 365
}
// 毫秒转秒
export function millisecond2Second(millisecond) {
return Math.round(millisecond / 1000)
}
// 计算时间戳之间的秒差
export function relativeSeconds(startStamp, endStamp) {
if (endStamp) {
return millisecond2Second(endStamp - startStamp)
} else {
const endStamp = new Date().valueOf()
return millisecond2Second(endStamp - startStamp)
}
}
// 秒转时分秒
export function seconds2Date(seconds) {
const dateObj = {
h: 0,
m: 0,
s: 0
}
if (seconds < DATE_CONFIG.m) {
dateObj.s = seconds
return dateObj
} else if (seconds < DATE_CONFIG.h) {
dateObj.m = Math.round(seconds / DATE_CONFIG.m)
return dateObj
} else if (seconds < DATE_CONFIG.d) {
dateObj.h = Math.round(seconds / DATE_CONFIG.h)
return dateObj
}
return dateObj
}
// 获取两个日期之间的相隔天数
export function relativeDays(startStamp, endStamp) {
return new Date(endStamp).diff(new Date(startStamp), 'day')
}
// 获取两个日期之间的相隔多少年
export function relativeYears(startStamp, endStamp) {
return new Date(endStamp).year() - new Date(startStamp).year()
}
/**
* 人性化显示时间
*
* @param {number | string} date 时间(时间戳或者字符)默认是时间戳
* @param {object} [options] 配置项(可选)
* @param {boolean} options.isString 指定date类型:默认是false. false:数字时间戳 true: YYYY-MM-DD HH:mm:ss (ISO 8601 string)
* @param {number} options.adjustVal 校正值。单位秒。即两时间相差{adjustVal}显示为刚刚。默认:0
*/
export function getHumanDate(date, options = {}) {
const { isString, adjustVal = 0, needYesterday = false, monthAndDay = false } = options
const startTemp = date
const currDate = new Date().valueOf()
const relativeS = relativeSeconds(startTemp, currDate)
if (Math.abs(relativeS) <= adjustVal) {
return '刚刚'
}
// 时间戳可能由服务器生成,会跟本地时间有误差。
if (relativeS <= 0 && Math.abs(relativeS) > adjustVal) {
return `未来 ${new Date(startTemp).format('yyyy-MM-dd hh:mm')}`
}
if (relativeS < DATE_CONFIG.m) {
return `${relativeS}秒前`
} else if (relativeS < DATE_CONFIG.h) {
return `${seconds2Date(relativeS).m}分钟前`
} else if (relativeS < DATE_CONFIG.d) {
return `${seconds2Date(relativeS).h}小时前`
} else if (needYesterday && relativeDays(startTemp, currDate) === 1) {
return `昨天 ${new Date(startTemp).format('hh:mm')}`
} else if (needYesterday && relativeDays(startTemp, currDate) === 2) {
return `前天 ${new Date(startTemp).format('hh:mm')}`
} else if (monthAndDay && relativeYears(currDate, startTemp) === 0) {
return new Date(startTemp).format('MM-dd hh:mm')
} else {
return new Date(startTemp).format('yyyy-MM-dd')
}
}