import dayjs from 'dayjs'; export default { format(date, template) { return dayjs(date).format(template); }, formatH(date) { if (date) { return this.format(date, 'HH:mm:ss'); } else { return '--:--:--'; } }, formatYM(date) { if (date) { return this.format(date, 'YYYY-MM'); } else { return '----/--'; } }, formatYMD(date) { if (date) { return this.format(date, 'YYYY-MM-DD'); } else { return '----/--/--'; } }, formatYMDHM(date) { if (date) { return this.format(date, 'YYYY-MM-DD HH:mm'); } else { return '----/--/-- --:--'; } }, formatYMDHMS(date) { if (date) { return this.format(date, 'YYYY-MM-DD HH:mm:ss'); } else { return '----/--/-- --:--:--'; } }, formatDateFromExcel(num, format) { const old = num - 1; const t = Math.round((old - Math.floor(old)) * 24 * 60 * 60); const time = new Date(1900, 0, old, 0, 0, t); const year = time.getFullYear(); const month = time.getMonth() + 1; const date = time.getDate(); return dayjs( year + format + (month < 10 ? '0' + month : month) + format + (date < 10 ? '0' + date : date) ); }, /** * 将秒数转换为中文描述格式 * @param {number} seconds - 秒数 * @returns {string} 中文时间描述 */ formatSecondsToChinese(seconds) { if (!seconds || seconds < 0 || isNaN(seconds)) { return '--'; } // 定义时间单位和对应的秒数 const units = [ { unit: '天', value: 24 * 60 * 60 }, { unit: '小时', value: 60 * 60 }, { unit: '分钟', value: 60 }, { unit: '秒', value: 1 } ]; let remainingSeconds = Math.floor(seconds); let result = ''; // 遍历时间单位,计算每个单位的数量 for (const { unit, value } of units) { if (remainingSeconds >= value) { const count = Math.floor(remainingSeconds / value); result += `${count}${unit}`; remainingSeconds %= value; // // 如果剩余秒数为0,且已经有结果,就可以结束了 // if (remainingSeconds === 0 && result) { // break; // } // 如果已经有结果,就可以结束了 if (result) { break; } } } return result; } };