diff --git a/src/assets/styles/index.scss b/src/assets/styles/index.scss index 12d8850..ce960bd 100644 --- a/src/assets/styles/index.scss +++ b/src/assets/styles/index.scss @@ -139,6 +139,7 @@ aside { //main-container全局样式 .app-container { + height: calc(100vh - 132px); padding: 8px 20px 20px; } diff --git a/src/components/echartsList/dateTimeSelect.vue b/src/components/echartsList/dateTimeSelect.vue index 36570a7..ceb8e75 100644 --- a/src/components/echartsList/dateTimeSelect.vue +++ b/src/components/echartsList/dateTimeSelect.vue @@ -88,30 +88,41 @@ }, // 获取前一个月的所有日期 getDaysOfPreviousMonth(star, end) { + // 1. 获取当前日期(终点日期) const currentDate = new Date(); - const currentYear = currentDate.getFullYear(); - const currentMonth = currentDate.getMonth(); // 0-11 表示 1-12 月 + const endYear = currentDate.getFullYear(); + const endMonth = currentDate.getMonth(); // 0=1月,11=12月 + const endDay = currentDate.getDate(); // 当前日(如8) - // 计算前一个月的年份和月份 - const prevMonth = currentMonth - 1; - const prevYear = prevMonth >= 0 ? currentYear : currentYear - 1; - const prevMonthActual = prevMonth >= 0 ? prevMonth : 11; // 11 表示 12 月 + // 2. 计算起点日期(当前日期的“上月同日”,处理边界:如3月31日→2月28/29日) + const startDate = new Date(endYear, endMonth, endDay); + // 核心:将当前日期的月份减1(得到上月同日,自动处理天数不足问题) + startDate.setMonth(startDate.getMonth() - 1); - // 获取第一天和最后一天 - const firstDay = star ? new Date(star) : new Date(prevYear, prevMonthActual, 1); - const lastDay = end ? new Date(end) : new Date(prevYear, prevMonthActual + 1, 0); // 下个月首日减 1 天 + // 3. 提取起点日期的年、月、日(用于循环判断) + const startYear = startDate.getFullYear(); + const startMonth = startDate.getMonth(); + const startDay = startDate.getDate(); - // 生成所有日期 - const oneMonthDays = []; - let currentDay = firstDay; - while (currentDay <= lastDay) { - // 格式化为 YYYY-MM-DD - const formattedDate = currentDay.toISOString().split('T')[0]; - oneMonthDays.push(formattedDate); - currentDay = new Date(currentDay); // 避免引用同一对象 - currentDay.setDate(currentDay.getDate() + 1); + // 4. 循环生成“起点~终点”的所有日期 + const dateCollection = []; + // 临时日期:从起点日期开始递增 + const tempDate = new Date(startYear, startMonth, startDay); + + // 循环条件:临时日期 <= 当前日期(终点) + while (tempDate <= currentDate) { + // 格式化日期为“YYYY-MM-DD”(补零处理:如8月→08,5日→05) + const year = tempDate.getFullYear(); + const month = String(tempDate.getMonth() + 1).padStart(2, '0'); + const day = String(tempDate.getDate()).padStart(2, '0'); + const formattedDate = `${year}-${month}-${day}`; + + dateCollection.push(formattedDate); + + // 临时日期加1天(进入下一天) + tempDate.setDate(tempDate.getDate() + 1); } - let timeArr = [oneMonthDays[0], oneMonthDays[oneMonthDays.length - 1]]; + let timeArr = [dateCollection[0], dateCollection[dateCollection.length - 1]]; this.$emit("dataChange", this.isActive, [], timeArr); // this.$emit("dataChange", this.isActive, oneMonthDays); }, @@ -120,7 +131,7 @@ const threeMonths = []; const today = new Date(); // 当前日期 const currentYear = today.getFullYear(); - const currentMonth = today.getMonth(); // 月份从0开始(0=1月,11=12月) + const currentMonth = today.getMonth() + 1; // 月份从0开始(0=1月,11=12月) // 循环获取前3个月(i=0: 前1个月,i=1: 前2个月,i=2: 前3个月) for (let i = 1; i <= num; i++) { // 计算目标月份(当前月 - i) diff --git a/src/components/table/index.vue b/src/components/table/index.vue index ba9463b..1216120 100644 --- a/src/components/table/index.vue +++ b/src/components/table/index.vue @@ -1,5 +1,5 @@