tanyanfei 6 months ago
parent
commit
a50ad131fd
1 changed files with 22 additions and 14 deletions
  1. 22 14
      pages/index/index.js

+ 22 - 14
pages/index/index.js

@@ -73,24 +73,18 @@ Page({
         match_day:day,
       })
       $api.getAvg({match_id:res.data.data[0].match_id}).then(res=>{
-        var week=['一','二','三','四','五','六','七','八','九','十','十一','十二','十三','十四','十五']
-        var days=res.data.data.list,weeks=[[]]
+        var days=res.data.data.list,weeks=[]
         days=days.reverse()
-        week.forEach((item,index)=>{
-          weeks[index]={
-            name:'第'+item+'周',
+        for(let i=0;i<52;i++){
+          weeks.push({
+            name:'第'+(i+1)+'周',
             data:[],
             total:0
-          }
-        })
-        var i=0
+          })
+        }
         days.forEach(item=>{
-          var w=new Date(item.stock_date).getDay() 
-          item.day=w
-          if(w==1){
-            i++
-          }
-          weeks[i].data.push(item)
+          var w=this.getWeekNumber(item.stock_date)
+          weeks[w-1].data.push(item)
         })
         weeks.forEach(item=>{
           if(item.data.length>0){
@@ -141,6 +135,20 @@ Page({
       })
     })
   },
+  //计算周数
+  getWeekNumber:function(dateString) {
+    const date = new Date(dateString);
+    const year = date.getFullYear();
+    // 将年份的第一个周一作为第一周的开始
+    const firstWeekStart = new Date(year, 0, 1);
+    firstWeekStart.setDate(firstWeekStart.getDate() - firstWeekStart.getDay() + 1);
+    // 计算给定日期是一年中的第几周
+    const currentDate = new Date(date);
+    currentDate.setDate(currentDate.getDate() - currentDate.getDay() + 1);
+    let milliseconds = currentDate.getTime() - firstWeekStart.getTime();
+    const weeks = Math.round(milliseconds / (7 * 24 * 60 * 60 * 1000));
+    return weeks + 1; // 因为getWeek返回的是年初至今的周数,所以加1得到完整的周数
+  },
   //获取今日留言
   getExp:function(){
     var list = this.data.explist