// 格式化浮点数 export function formatFloat(number: string | number, precision = 2) { number = parseFloat(number); if (isNaN(number) && !number) return number; return number.toFixed(precision); } export function fixFloat(number: number, digit = 2) { const m = 10 ** digit; return (Math.round(number * m, 10) / m).toFixed(digit); } export function toMoney(num: string | number) { num = fixFloat(num); if (isNaN(num)) return num; const [i, f] = String(num).split('.'); return `${parseFloat(i).toLocaleString()}.${f}`; } // 格式化日期 export function formatDate(date: string | number | Date, fmt = 'yyyy-MM-dd hh:mm:ss') { const that = new Date(date); const o = { 'M+': that.getMonth() + 1, // 月份 'd+': that.getDate(), // 日 'h+': that.getHours(), // 小时 'm+': that.getMinutes(), // 分 's+': that.getSeconds(), // 秒 'q+': Math.floor((that.getMonth() + 3) / 3), // 季度 S: that.getMilliseconds(), // 毫秒 }; if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (`${that.getFullYear()}`).substr(4 - RegExp.$1.length)); } for (const k in o) { if (new RegExp(`(${k})`).test(fmt)) { fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : ((`00${o[k]}`).substr((`${o[k]}`).length))); } } return fmt; } export function calculateDateIntervals(StartTime1: string | number | Date, EndTime2: string | number | Date) { const currentDate = new Date(); let startDate, endDate; const dateNow = currentDate.getDate(); if (!StartTime1 && !EndTime2) { startDate = new Date(currentDate.getFullYear(), currentDate.getMonth() - 11, dateNow); endDate = currentDate; const monthList = []; const type = 2; for (let month = 0; month <=11; month++) { const date = new Date(currentDate.getFullYear(), currentDate.getMonth() - month, dateNow); monthList.push(date.toISOString().slice(0, 7)); } const reversedArray = []; for (let i = monthList.length - 1; i >= 0; i--) { reversedArray.push(monthList[i]); } // console.log(reversedArray) return { type, list: reversedArray }; } else { startDate = new Date(StartTime1); endDate = new Date(EndTime2); } const timeDiff = endDate.getTime() - startDate.getTime(); const oneDay = 24 * 60 * 60 * 1000; // 一天的毫秒数 const daysDiff = Math.floor(timeDiff / oneDay); if (daysDiff < 15) { const dateList = []; const type = 3; for (let i = 0; i <= daysDiff; i++) { const day = new Date(startDate.getTime() + i * oneDay); dateList.push(day.toISOString().split('T')[0]); } return { type, list: dateList }; } else if (daysDiff <= 365) { const monthList = []; const type = 2; const startYear = startDate.getFullYear(); const endYear = endDate.getFullYear(); const startMonth = startDate.getMonth(); const endMonth = endDate.getMonth(); for (let year = startYear; year <= endYear; year++) { const monthStart = (year === startYear) ? startMonth : 0; const monthEnd = (year === endYear) ? endMonth : 11; for (let month = monthStart; month <= monthEnd; month++) { monthList.push(`${year}-${String(month + 1).padStart(2, '0')}`); } } // const reversedArray = []; // for (let i = monthList.length - 1; i >= 0; i--) { // reversedArray.push(monthList[i]); // } console.log(monthList) return { type, list: monthList }; } else { const yearList = []; const type = 1; for (let year = startDate.getFullYear(); year <= endDate.getFullYear(); year++) { yearList.push(year.toString()); } return { type, list: yearList }; } } export function getPreviousMonths() { const currentDate = new Date(); const previousMonths = []; for (let i = 0; i < 5; i++) { const previousMonth = new Date(currentDate.getFullYear(), currentDate.getMonth() - (i+2), 1); previousMonths.push(previousMonth.toLocaleString('default', { month: 'long', year: 'numeric' })); } return previousMonths.reverse(); }