js根据日期判断昨天今天并显示(Vue)

应最近项目要求,按照日期判断昨天和今天,如果是昨天或今天显示汉字,否则显示日期

效果图
js根据日期判断昨天今天并显示(Vue)_第1张图片

 
{{judgeTime('2019-03-14 09:11:18')}}
add0 (m) { return m < 10 ? '0' + m : m }, judgeTime (date) { console.log(date) // 2019-03-14 09:11:18 let _this = this let dateStr = new Date(date) let today = new Date(parseInt(this.serverTime)) let hour = today.getHours() let minute = today.getMinutes() let second = today.getSeconds() today.setHours(0) today.setMinutes(0) today.setSeconds(0) today.setMilliseconds(0) let otime = today.getTime() // 给出时间 - 今天0点 let offset = dateStr.getTime() - otime let isToday = offset / 1000 / 60 / 60 if (isToday > 0 && isToday <= 24) { return '今天 ' + _this.add0(hour) + ':' + _this.add0(minute) + ':' + _this.add0(second) } else if (isToday < 0 && isToday >= -24) { return '昨天 ' + _this.add0(hour) + ':' + _this.add0(minute) + ':' + _this.add0(second) } else { return date } }

你可能感兴趣的:(前端之vue,JS)