模仿微信聊天页的时间格式
代码写的不好,但基本上能实现功能
/**
* 把时间戳转为时间,一周内显示周,一天内显示时分
* @param {时间戳} time
*/
transTime (time) {
let toDay = (new Date()).getDate() // 今天是哪号
let timeDay = (new Date(time)).getDate() // 时间缀转为具体的哪一号
var toYear = (new Date()).getFullYear() // 获取年
var timeYear = (new Date(time)).getFullYear() // 获取年
var toMonth = (new Date()).getMonth() + 1 // 获取月
var timeMonth = (new Date(time)).getMonth() + 1 // 获取月
let myMonth = toMonth - timeMonth
let toHours = (new Date()).getHours() // 获取小时
let timeHours = (new Date(time)).getHours() // 获取小时
let Minutes = (new Date()).getMinutes() // 获取分钟
let timeMinutes = (new Date(time)).getMinutes() // 获取分钟
if (timeHours < 10) {
timeHours = '0' + timeHours
}
if (Minutes < 10) {
Minutes = '0' + Minutes
}
// console.log(toYear, timeYear)
if (toYear - timeYear > 0) {
let tm = timeMonth
if (tm < 10) {
tm = ('0' + tm)
}
let td = timeDay
if (td < 10) {
td = ('0' + td)
}
// console.log('一年前')
// console.log(timeYear + '-' + timeMonth + '-' + timeDay)
return (timeYear + '-' + tm + '-' + td + ' ' + timeHours + ':' + Minutes)
}
// 大于一周
// console.log(toDay, timeDay)
if ((myMonth === 0) && ((toYear - timeYear) === 0)) {
// 本月
// 一周内的
if ((toDay - timeDay) === 0) {
// 日期是今天的
// 一个小时内3分钟前的
// 15分内3分前的
if ((toHours - timeHours) === 0) {
// 一个小时内
let xz = ((new Date()) - time) / 60000
let fz = Math.floor(xz)
if (fz > 3) {
return Math.floor(xz) + '分钟前'
}
} else {
// 大于一个小时
if (timeMinutes < 10) {
timeMinutes = '0' + timeMinutes
}
return (timeHours + ':' + timeMinutes)
}
} else if (((toDay - timeDay) >= 1) && (toDay - timeDay <= 7)) {
// 1周内的
let weekTime = (new Date(time)).getDay()
let weekD
if (weekTime === 0) weekD = '星期日'
if (weekTime === 1) weekD = '星期一'
if (weekTime === 2) weekD = '星期二'
if (weekTime === 3) weekD = '星期三'
if (weekTime === 4) weekD = '星期四'
if (weekTime === 5) weekD = '星期五'
if (weekTime === 6) weekD = '星期六'
// console.log(weekD)
return (weekD + ' ' + timeHours + ':' + Minutes)
// console.log('昨天')
} else {
// 大于一周显示日期(如:3月25日
let tm = timeMonth
if (tm < 10) {
tm = ('0' + tm)
}
let td = timeDay
if (td < 10) {
td = ('0' + td)
}
return (timeYear + '-' + tm + '-' + td + ' ' + timeHours + ':' + Minutes)
}
}
if ((myMonth > 0) && ((toYear - timeYear) === 0)) {
// 大于1个月,同一年的
let tm = timeMonth
if (tm < 10) {
tm = ('0' + tm)
}
let td = timeDay
if (td < 10) {
td = ('0' + td)
}
return (timeYear + '-' + tm + '-' + td + ' ' + timeHours + ':' + Minutes)
}
},