function formatMsgTime(timespan) { var dateTime = parseDate(timespan); var year = dateTime.getFullYear(); var month = dateTime.getMonth() + 1; var day = dateTime.getDate(); var hour = dateTime.getHours(); var minute = dateTime.getMinutes(); minute = minute < 10 ? "0" + minute : minute var second = dateTime.getSeconds(); var now = new Date(); var milliseconds = 0; var timeSpanStr = ""; milliseconds = now.getTime() - dateTime.getTime(); var when = ""; if (hour < 6) when = "凌晨 " else if (hour < 9) when = "早上 " else if (hour < 12) when = "上午 " else if (hour < 14) when = "中午 " else if (hour < 17) when = "下午 " else if (hour < 19) when = "傍晚 " else if (hour < 22) when = "晚上 " else when = "深夜 " if (milliseconds <= 1000 * 30 * 1) { timeSpanStr = '刚刚'; } else if (milliseconds <= 1000 * 60 * 1) { timeSpanStr = '1分钟内'; } else if (1000 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 30) { timeSpanStr = Math.round((milliseconds / (1000 * 60))) + '分钟前'; } else if (1000 * 60 * 30 < milliseconds && milliseconds <= 1000 * 60 * 60) { timeSpanStr = '半小时前'; } else if (1000 * 60 * 60 < milliseconds && milliseconds <= (1000 * 60 * 60 * now.getHours() + 1000 * 60 * now.getMinutes() + 1000 * now.getSeconds() + now.getMilliseconds())) { timeSpanStr = "今天" + when + hour + ":" + minute; } else if (1000 * 60 * 60 * now.getHours() < milliseconds && milliseconds <= 1000 * 60 * 60 * 24) { timeSpanStr = '昨天' + when + hour + ":" + minute; } else if (1000 * 60 * 60 * 24 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24 * 15) { var d = Math.round(milliseconds / (1000 * 60 * 60 * 24)); if (d == 1 || (d == 2 && milliseconds == (1000 * 60 * 60 * now.getHours() + 1000 * 60 * now.getMinutes() + 1000 * now.getSeconds() + now.getMilliseconds()) + 86400000)) { timeSpanStr += '昨天' } else if (d == 2) { timeSpanStr += '前天' } else if (d == 3) { timeSpanStr += '大前天' } else { timeSpanStr += Math.round(milliseconds / (1000 * 60 * 60 * 24)) + '天前'; } timeSpanStr += when + hour + ":" + minute } else if (milliseconds > 1000 * 60 * 60 * 24 * 15 && year == now.getFullYear()) { timeSpanStr = month + '月' + day + '日 ' + hour + ':' + minute; } else { timeSpanStr = year + '年' + month + '月' + day + '日 ' + hour + ':' + minute; } return timeSpanStr;}
var parseDate = function (dateString) { return new Date(Date.parse(dateString.replace(/-/g, "/")));};