前端js计算距离现在的时间差,返回XX年前XX个月前XX天前XX
距离现在的时间差
var getTime = function (date) {
if (!date) {
return
}
var nowTime = getDate().getTime()
var front = date.split(' ')[0].split('-')
var end = date.split(' ')[1].split(':')
var pubTime = getDate(front[0], front[1] - 1, front[2], end[0], end[1], end[2]).getTime()
var diff = nowTime - pubTime
var str = ''
var year = diff / ( 1000 * 60 * 60 * 24 * 365 )
var month = diff / ( 1000 * 60 * 60 * 24 * 30 )
var day = diff / ( 1000 * 60 * 60 * 24 )
var hour = diff / ( 1000 * 60 * 60 )
var min = diff / ( 1000 * 60 )
if (year >= 1) {
str = Math.floor(year) + '年前'
} else if (month >= 1) {
str = Math.floor(month) + '个月前'
} else if (day >= 1) {
str = Math.floor(day) + '天前'
} else if (hour >= 1) {
str = Math.floor(hour) + '小时前'
} else if (min >= 1) {
str = Math.floor(min) + '分钟前'
} else {
str = "刚刚"
}
return str
}
module.exports = {
getTime: getTime
}