function TimeFormatting(dateTimeStamp) { var result; var minute = 1000 * 60; var hour = minute * 60; var day = hour * 24; var halfamonth = day * 15; var month = day * 30; var now = new Date(); //当前时间 // console.log('now', now); var digit = String(dateTimeStamp).length; if (digit == '10') { var curTime = new Date(parseInt(dateTimeStamp) * 1000); //后端返回的是秒数 过去时间 } else { var curTime = new Date(parseInt(dateTimeStamp)); //后端返回的是秒数 过去时间 } // console.log('curTime',curTime); var _year = now.getFullYear(); // 年 var _month = now.getMonth() + 1; //月 var _day = now.getDate(); //日 var shitime = _year + '-' + _month + '-' + _day + ' 23:59:59'; // console.log(shitime); var time_start = new Date(shitime.replace(/-/g, '/')); time_start.getTime(); var diffValue = time_start.getTime() - curTime; // 过去时间和现在时间对比 相差多少天 var CutOffDays = parseInt(diffValue / day); var Now_hour = now.getHours(); //当前 小时数 var Now_mm = now.getMinutes(); //当前 分 var Current_hour = curTime.getHours(); //过去 小时数 var Current_mm = curTime.getMinutes(); //过去 分 var Differhour = Now_hour - Current_hour //过去时间和现在时间相差小时 var DifferMinute = Now_mm - Current_mm //过去时间和现在时间相差分钟 // console.log(Differhour) // console.log(DifferMinute) var timestr = ''; if (Current_hour < 11) { timestr = " 早上 "; } else if (Current_hour < 13) { timestr = " 中午 "; } else if (Current_hour < 17) { timestr = " 下午 "; } else if (Current_hour < 19) { timestr = " 晚上 "; } else { timestr = " 晚上 "; } // 日 var currentTimeDay = curTime.getDate(); //历史记录年月日 var SpecificDate = curTime.getFullYear() + '年' + (curTime.getMonth() + 1) + '月' + currentTimeDay + '日 '; // console.log(SpecificDate); //历史记录时间 var Historicaltime = curTime.getHours() + ':' + (curTime.getMinutes() >= 10 ? curTime.getMinutes() : ('0' + curTime.getMinutes())); // console.log(Historicaltime); var weekTime = (new Date(curTime)).getDay(); //星期 工作日 // console.log(weekTime); var week = ""; // console.log(CutOffDays, '2222'); if (CutOffDays > 7) { // 消息超过 7天 显示日期 result = SpecificDate + timestr + Historicaltime; } else if (CutOffDays < 1) { // 消息小于 1天 result = timestr + Historicaltime; } else if (CutOffDays >= 1 && CutOffDays < 2) { // 消息大于 1天 并且 小于 2天 显示昨天 result = '昨天' + timestr + Historicaltime; } else if (CutOffDays >= 2 && CutOffDays <= 7) { // 消息大于 2天 并且 小于 7天 显示星期 // console.log(weekTime); switch (weekTime) { case 0: week = '星期日' break; case 1: week = '星期一' break; case 2: week = '星期二' break; case 3: week = '星期三' break; case 4: week = '星期四' break; case 5: week = '星期五' break; case 6: week = '星期六' break; } result = week + timestr + Historicaltime; } // console.log(result); return result; } //最下面一定要加上你自定义的方法(作用:将模块接口暴露出来),否则会报错:util.trim is not a function; module.exports = { TimeFormatting: TimeFormatting, }