方法内代码:参数date为时间戳
let that = this; if (!date) { return; } date = date * 1000 const IN_DAY = 1000 * 60 * 60 * 24 * 1; //1天 const Yesterday = 1000 * 60 * 60 * 24 * 2; //昨天 let Y = dayjs(date).year(), M = dayjs(date).month(), D = dayjs(date).date(), // W = dayjs(date).day(), W = dayjs(date).format('ddd'), H = dayjs(date).hour(), Min = dayjs(date).format('mm'), nowY = dayjs().year(), nowM = dayjs().month(), nowD = dayjs().date(), timeStr = ''; if (Y < nowY) { // 去年 timeStr = `${y}/${M}/${D} ` } else { if (Y === nowY && M === nowM) { // 昨天 if ((nowD - D) === 1) { timeStr = '昨天 ' } else if (nowD === D) { // 当天 timeStr = '' } else { // 一周内 timeStr = W + ' ' } } else { timeStr = `${M}/${D} ` } } timeStr += `${H}:${Min}` return timeStr
npm地址:https://www.npmjs.com/package/dayjs
npm安装:
npm install dayjs --save