通话时间
--1分钟之内显示“刚刚”;
—超过1分钟,在1天之内显示具体时间,时+分(24小时制),如“14:10”;
--前1天显示“昨天+时分”
--超过1天且在7天之内显示“星期X+时分”。
--超过7天的显示具体日期“年/月/日+时分”
export function timestampFormat (timestamp) {
function zeroize (num) {
return (String(num).length === 1 ? '0' : '') + num
}
var curTimestamp = parseInt(new Date().getTime())
var timestampDiff = curTimestamp - timestamp
console.log(timestampDiff, 'timestampDiff')
var curDate = new Date(curTimestamp)
var tmDate = new Date(timestamp)
var Y = tmDate.getFullYear()
var m = tmDate.getMonth() + 1
var d = tmDate.getDate()
var H = tmDate.getHours()
var i = tmDate.getMinutes()
if (timestampDiff < 60000) {
return '刚刚'
} else if (curDate.getFullYear() === Y && curDate.getMonth() + 1 === m && curDate.getDate() === d) {
return zeroize(H) + ':' + zeroize(i)
} else if (timestampDiff < 172800000) {
return '昨天 ' + zeroize(H) + ':' + zeroize(i)
} else if (timestampDiff < 604800000) {
const days = ['日', '一', '二', '三', '四', '五', '六']
return '星期' + days[tmDate.getDay()] + ' ' + zeroize(H) + ':' + zeroize(i)
} else {
return Y + '/' + m + '/' + d + ' ' + H + ':' + i
}
}