function formatData(val) { return getDate(val * 1000).toLocaleString() } function formatArr(val) { var special_confArr = val.toString(2).split('').slice(0, val.toString(2).length - 1).reverse() return getInital(special_confArr) } function getDateDiff(now, dateTime) { var result = ''; var minute = 1000 * 60; var diffValue = now - dateTime; if (diffValue < 0) { return result = "刚刚"; } var minEnd = diffValue / minute; var date = getDate(dateTime); var nowdate = getDate(now); var y = date.getFullYear(); var ny = nowdate.getFullYear(); var m = formatNumber(date.getMonth() + 1); var nm = formatNumber(nowdate.getMonth() + 1); var w = date.getDay(); var d = formatNumber(date.getDate()); var nd = formatNumber(nowdate.getDate()); var h = formatNumber(date.getHours()); var i = formatNumber(date.getMinutes()); var dayEnd; var yearEnd = ny - y; var year; if (nm - m == 1) { switch (parseInt(m)) { case 01: dayEnd = parseInt(nd) + 31 - parseInt(d); break; case 02: if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0) { year = 29; } else { year = 28; } dayEnd = parseInt(nd) + year - parseInt(d); break; case 03: dayEnd = parseInt(nd) + 31 - parseInt(d); break; case 04: dayEnd = parseInt(nd) + 30 - parseInt(d); break; case 05: dayEnd = parseInt(nd) + 31 - parseInt(d); break; case 06: dayEnd = parseInt(nd) + 30 - parseInt(d); break; case 07: dayEnd = parseInt(nd) + 31 - parseInt(d); break; case 08: dayEnd = parseInt(nd) + 31 - parseInt(d); break; case 09: dayEnd = parseInt(nd) + 30 - parseInt(d); break; case 10: dayEnd = parseInt(nd) + 31 - parseInt(d); break; case 11: dayEnd = parseInt(nd) + 30 - parseInt(d); break; default: break; } } else if (nm - m == 0) { dayEnd = nd - d; } else { dayEnd = 7 } if (yearEnd >= 1) { result = y + "年" + m + "月" + d + "日"; } else if (dayEnd >= 7 && yearEnd < 1) { result = m + "月" + d + "日"; } else if (dayEnd >= 2 && dayEnd < 7) { result = "" + getWeekDay(w); } else if (dayEnd >= 1 && dayEnd < 2) { result = "" + "昨天" + "" + h + ':' + i; } else if (minEnd >= 60 && dayEnd < 1) { result = "" + h + ':' + i; } else if (minEnd >= 1 && minEnd < 60) { result = "" + parseInt(minEnd) + "分钟前"; } else { result = "刚刚"; } return result; }; function formatNumber(n) { n = n.toString() return n[1] ? n : '0' + n } function getWeekDay(index) { var show_day = ["星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]; return show_day[index] } function getInital(arr) { var result = [] for (var i = 0; i < arr.length; i++) { if (arr[i] == 1) { result.push(i + 1 + '') } } return result } function transitionTime(val, date = "") { var time = getDate(val * 1000).toLocaleString() var reg = getRegExp(':', 'g'); var timearr = time.replace(" ", ":").replace(reg, "/").split("/"); var timestr = date ? timearr[0] + "年" : timearr[0] + "年" + timearr[1] + "月" + timearr[2] + "日"; return timestr } module.exports = { formatData: formatData, formatArr: formatArr, transitionTime: transitionTime, getDateDiff: getDateDiff }