//常用于论坛中,例如发表评论后显示刚刚
//规则如下(可根据实际情况进行更改)
1:十分钟内返回 “”“刚刚”
2: 今天内 --- 显示多少小时前
3: 昨天内 ----昨天 小时:分钟
4 : 再往前一致显示为 16-11-11 11:11
//时间转换函数,传入时间戳 //传入时间戳 function getDateDiff(dateTimeStamp) { var currentTime = {}; var result = ""; var now = new Date(); //当前时间 currentTime.currentYear = now.getFullYear(); currentTime.currentMonth = now.getMonth() + 1; currentTime.currentDay = now.getDay(); currentTime.currentHour = now.getHours(); currentTime.currentMinute = now.getMinutes(); var thisTime = new Date(dateTimeStamp); var thisNow = {}; thisNow.year = thisTime.getFullYear(); thisNow.month = thisTime.getMonth() + 1; thisNow.day = thisTime.getDay(); thisNow.hour = (thisTime.getHours() < 10 ? ("0" + thisTime.getHours()) : thisTime.getHours()); thisNow.minute = (thisTime.getMinutes() < 10 ? ("0" + thisTime.getMinutes()) : thisTime.getMinutes()); if (now.getTime() - thisTime.getTime() < 0) { return; } var isSameYear = (currentTime.currentYear == thisNow.year) ? true : false; var isSameMonth = (currentTime.currentMonth == thisNow.month) ? true : false; var isSameDay = (currentTime.currentDay == thisNow.day) ? true : false; var isSameHour = (currentTime.currentHour == thisNow.hour) ? true : false; if (isSameYear && isSameMonth && isSameDay) { if (isSameHour) { //同年同月同时 var minuteSum = currentTime.currentMinute - thisNow.minute; if (minuteSum <= 10) { result = "刚刚" } else if (minuteSum < 60) { result = minuteSum + "分钟前"; } } else { result = (currentTime.currentHour - thisNow.hour) + "小时前"; } } else if (isSameYear && isSameMonth && !isSameDay) { //同年同月不同天 var daySum = currentTime.currentDay - thisNow.day; if (daySum <= 1) { result = "昨天" + thisNow.hour + ":" + thisNow.minute; } else { result = "16-11-11 11:11"; } } else { result = "16-11-11 11:11"; } return result; }