//显示日期 //时间计算 export function showtime(time) { let date = typeof time === "number" ? new Date(time) : new Date((time || "").replace(/-/g, "/")); let diff = (new Date().getTime() - date.getTime()) / 1000; let dayDiff = Math.floor(diff / 86400); let year = new Date().getFullYear() let nowyear = new Date(time).getFullYear() let isValidDate = Object.prototype.toString.call(date) === "[object Date]" && !isNaN(date.getTime()); if (!isValidDate) { window.console.error("不是有效日期格式"); } // const formatDate = function(date) { // let today = new Date(date); // let year = today.getFullYear(); // let month = ("0" + (today.getMonth() + 1)).slice(-2); // let day = ("0" + today.getDate()).slice(-2); // let hour = today.getHours(); // let minute = today.getMinutes(); // let second = today.getSeconds(); // return `${year}-${month}-${day} ${hour}:${minute}:${second}`; // }; // if (isNaN(dayDiff) || dayDiff < 0 || dayDiff >= 31) { // return formatDate(date); // } return ( (dayDiff === 0 && ((diff < 60 && "刚刚") || (diff < 120 && "1分钟前") || (diff < 3600 && Math.floor(diff / 60) + "分钟前") || (diff < 7200 && "1小时前") || (diff < 86400 && Math.floor(diff / 3600) + "小时前"))) || (dayDiff === 1 && "昨天") || (dayDiff < 7 && dayDiff + "天前") || // (dayDiff < 31 && Math.ceil(dayDiff / 7) + "周前")|| (year == nowyear && time.slice(5)) || (year > nowyear && time) ); } //获取当前周数 export function getWeekInYear(date) { // console.log(date) //判断该星期是否跨年,如果跨年就是第一周 let weekStartDate = getWeekStartByDate(date) //一周开始时间 // console.log(weekStartDate,'weekStartDate') let endDate = getDateFromDay(weekStartDate, 6) //一周结束时间 // console.log(endDate,'endDate') if (weekStartDate.getFullYear() != endDate.getFullYear()) { // console.log(endDate.getFullYear() + '年第1周') let num = 1 return num } let d1 = new Date(date) let d2 = new Date(date) d2.setMonth(0) d2.setDate(1) d2 = getWeekStartByDate(d2) // console.log(d2,'d2') let rq = d1 - d2 let days = Math.ceil(rq / (24 * 60 * 60 * 1000)) + 1 let num = Math.ceil(days / 7) // console.log(endDate.getFullYear() + '年第' + num + '周') return num } function getWeekStartByDate(date) { let day = new Date(date).getDay() == 0 ? 6 : new Date(date).getDay() - 1 return getDateFromDay(date, -day) } function getDateFromDay(dayDate, day) { let date = new Date() date.setTime(new Date(dayDate).getTime() + day * 24 * 60 * 60 * 1000) return date } //获取本周一周日期 export function getNowWeekList(data) { let now = new Date(data); // 当前日期 let nowTime = now.getTime() let nowDayOfWeek = now.getDay() == 0 ? 6 : now.getDay() - 1; // 今天本周的第几天 let dayList = [] let startTime = uni.$u.timeFormat((nowTime - nowDayOfWeek * 86400000), 'yyyy-mm-dd'); let start = new Date(startTime).getTime() dayList.push(startTime) for (let i = 1; i < 7; i++) { dayList.push(uni.$u.timeFormat((start + i * 86400000), 'yyyy-mm-dd')) if (i == 6) { return dayList } } } //获取树形结构 export function handleTree(data, id, parentId, children) { let config = { id: id || 'id', parentId: parentId || 'parentId', childrenList: children || 'children' }; var childrenListMap = {}; var nodeIds = {}; var tree = []; for (let d of data) { let parentId = d[config.parentId]; if (childrenListMap[parentId] == null) { childrenListMap[parentId] = []; } nodeIds[d[config.id]] = d; childrenListMap[parentId].push(d); } for (let d of data) { let parentId = d[config.parentId]; if (nodeIds[parentId] == null) { tree.push(d); } } for (let t of tree) { adaptToChildrenList(t); } function adaptToChildrenList(o) { if (childrenListMap[o[config.id]] !== null) { o[config.childrenList] = childrenListMap[o[config.id]]; } if (o[config.childrenList]) { for (let c of o[config.childrenList]) { adaptToChildrenList(c); } } } return tree; }