// utils.js export function formatTime(time) { if (typeof time !== 'number' || time < 0) { return time.toString(); } const hour = Math.floor(time / 3600); time %= 3600; const minute = Math.floor(time / 60); const second = time % 60; return [hour, minute, second].map(n => n.toString().padStart(2, '0')).join(':'); } export function formatLocation(longitude, latitude) { if (typeof longitude === 'string' && typeof latitude === 'string') { longitude = parseFloat(longitude); latitude = parseFloat(latitude); } return { longitude: longitude.toFixed(2).split('.'), latitude: latitude.toFixed(2).split('.') }; } export const dateUtils = { UNITS: { '年': 31557600000, '月': 2629800000, '天': 86400000, '小时': 3600000, '分钟': 60000, '秒': 1000 }, humanize(milliseconds) { for (const key in this.UNITS) { if (milliseconds >= this.UNITS[key]) { return Math.floor(milliseconds / this.UNITS[key]) + key + '前'; } } return '刚刚'; }, format(dateStr) { const date = this.parse(dateStr); const diff = Date.now() - date.getTime(); if (diff < this.UNITS['天']) { return this.humanize(diff); } const _format = number => (number < 10 ? `0${number}` : number); return `${date.getFullYear()}/${_format(date.getMonth() + 1)}/${_format(date.getDate())} - ${_format(date.getHours())}:${_format(date.getMinutes())}`; }, parse(str) { // 将"yyyy-mm-dd HH:MM:ss"格式的字符串,转化为一个Date对象 const a = str.split(/[^0-9]/); return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]); } }; export function formateDates(datetime, type) { const year = datetime.getFullYear(); const month = String(datetime.getMonth() + 1).padStart(2, '0'); const date = String(datetime.getDate()).padStart(2, '0'); const hour = String(datetime.getHours()).padStart(2, '0'); const minute = String(datetime.getMinutes()).padStart(2, '0'); const second = String(datetime.getSeconds()).padStart(2, '0'); switch (type) { case "Y-M-D h:min:s": return `${year}-${month}-${date} ${hour}:${minute}:${second}`; case "Y-M-D": return `${year}-${month}-${date}`; case "h:min:s": return `${hour}:${minute}:${second}`; default: return ''; } } export function deteleObject(obj) { const uniques = []; const stringify = {}; obj.forEach(item => { const keys = Object.keys(item).sort(); const str = keys.map(key => JSON.stringify([key, item[key]])).join(''); if (!stringify[str]) { uniques.push(item); stringify[str] = true; } }); return uniques; } export function formateDate(datetime, type) { const year = datetime.getFullYear(); const month = String(datetime.getMonth() + 1).padStart(2, '0'); const date = String(datetime.getDate()).padStart(2, '0'); const hour = String(datetime.getHours()).padStart(2, '0'); const minute = String(datetime.getMinutes()).padStart(2, '0'); const second = String(datetime.getSeconds()).padStart(2, '0'); switch (type) { case "Y-M-D h:min:s": return `${year}-${month}-${date} ${hour}:${minute}:${second}`; case "Y-M-D": return `${year}年${month}月${date}日`; case "h:min:s": return `${hour}:${minute}:${second}`; case "h": return hour; case "min": return minute; default: return ''; } } export function randomNum(minNum, maxNum = null) { if (maxNum === null) { maxNum = minNum; minNum = 0; } return Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum; } export function pointInsideCircle(point, circle, r) { if (r === 0) return false; const dx = circle[0] - point[0]; const dy = circle[1] - point[1]; return dx * dx + dy * dy <= r * r; } export function isSameDay(timeStampA) { const dateA = new Date(timeStampA); const dateB = new Date(); return dateA.setHours(0, 0, 0, 0) === dateB.setHours(0, 0, 0, 0); } export function getCurrentMonthFirst() { const date = new Date(); date.setDate(1); return `${date.getFullYear()}年${date.getMonth() + 1}月${date.getDate()}日`; } export function getCurrentMonthLast() { const date = new Date(); date.setMonth(date.getMonth() + 1, 0); return `${date.getFullYear()}年${date.getMonth() + 1}月${date.getDate()}日`; }