简体   繁体   中英

Get Date & Day from Git commit api

I have called Git commit api, which gives array of 7 days which starts from Sunday, i want day & date from that array. eg. Sun 6 Aug Mon 7 Aug Tue 8 Aug .... I try to get this with following code which is suggested by 1 developer, but it gives only start day & date. Pls help me to achieve it.

Suggested code:

var data = data.json().data;
        if (data) {

            var months = {};
            for (var i = 0; i < data.length; i++) {
                console.log(data[i]);
                var monthNum = 0;
                if (data[i].week) {
                    console.log(new Date(data[i].week * 1000));
                    monthNum = new Date(data[i].week * 1000).getMonth();
                } else {
                    monthNum = new Date().getMonth()
                }
                if (!months[monthNum]) {
                    months[monthNum] = [];
                }
                months[monthNum].push(data[i]);
            }
            console.log(months);
        }

Api Response

    {  
   "success":true,
   "data":[  
      {  
         "days":[  
            0,
            0,
            0,
            0,
            0,
            0,
            0
         ],
         "total":0,
         "week":1471132800
      },
      {  
         "days":[  
            0,
            0,
            0,
            0,
            0,
            0,
            0
         ],
         "total":0,
         "week":1471737600
      },
      {  
         "days":[  
            0,
            0,
            2,
            4,
            0,
            0,
            0
         ],
         "total":6,
         "week":1472342400
      },
      {  
         "days":[  
            0,
            0,
            0,
            0,
            0,
            0,
            0
         ],
         "total":0,
         "week":1472947200
      },
      {  
         "days":[  
            0,
            0,
            0,
            0,
            0,
            0,
            0
         ],
         "total":0,
         "week":1473552000
      },
      {  
         "days":[  
            0,
            0,
            0,
            0,
            0,
            0,
            0
         ],
         "total":0,
         "week":1474156800
      },
      {  
         "days":[  
            0,
            0,
            0,
            0,
            0,
            0,
            0
         ],
         "total":0,
         "week":1474761600
      },
      {  
         "days":[  
            0,
            0,
            0,
            0,
            0,
            0,
            0
         ],
         "total":0,
         "week":1475366400
      },
      {  
         "days":[  
            0,
            0,
            0,
            0,
            0,
            0,
            0
         ]
    }

Is this the output you want? Run the code and see if this helps.

function getComitDates() {
    var data = data.json().data;
    if(data) {
        var weekFlat = data.map(function(m){
                   return m.days.map(function(n,k){
                       return {
                           week:m.week,
                           dayIndex:k,
                           sum:n
                       }
                   })
                });
        var options = {month: 'short', day: 'numeric'};
        var result = weekFlat.map(function(arr){
            return arr.map(function(ed){ 
                var date = ed.week?new Date((ed.week*1000) + ed.dayIndex*24*3600*1000) : new Date(); 
                return date.toLocaleDateString('en-US', options) + ' ' +ed.sum
            });
        }).join(",").split(",");
        console.log(result);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM