Hi,小弟我現在有一個資料,是按照每天計算出來的tp.對應的資料為
["2011-1-1","2011-1-2","2011-1-3","2011-1-4",...]
每一天對應的資料(tp)
[1,2,2,3,...]
那麼現在要講這些日期按照周分類,沒就是計算的結果為
["2011年第一周",...]
資料為
[8,...]
那麼現在該怎麼做呢?? 用什麼語言實現都無所謂~~~~
回複內容:
Hi,小弟我現在有一個資料,是按照每天計算出來的tp.對應的資料為
["2011-1-1","2011-1-2","2011-1-3","2011-1-4",...]
每一天對應的資料(tp)
[1,2,2,3,...]
那麼現在要講這些日期按照周分類,沒就是計算的結果為
["2011年第一周",...]
資料為
[8,...]
那麼現在該怎麼做呢?? 用什麼語言實現都無所謂~~~~
$date) { if($ret_list[date('W', strtotime($date))]) { $ret_list[date('W', strtotime($date))] += $num_list[$k]; } else { $ret_list[date('W', strtotime($date))] = $num_list[$k]; }}echo("日期數組:
");print_r($date_list);echo("
");echo("數值數組:
");print_r($num_list);echo("
");echo("按周統計數組(數組的鍵就是今年的第幾周):
");print_r($ret_list);
重點在於date函數的使用,如何擷取日期屬於年度的第幾周,看看php的doc吧。
var datas = ["2011-1-1","2011-1-2","2011-1-3","2011-1-4",...]function toWeekAarry(data){ var dateArr ,dateObj ,currWeek = 0; ,weeks = []; //假設樓主的資料不是連續的,而且不是全部元素合法 datas.filter(dateStr=>typeof dateStr==="string").forEach(dateStr=>{ dateArr = dateStr.split("-") if(dateArr.length===3){ dateObj = new Date(dateArr[0],dateArr[1],dateArr[2]) currWeek = getWeekOfDate(dateObj) if(!weeks[])weeks[currWeek]=[] weeks[currWeek].push(date); } }) retrun weeks.map(week=>if(!week)week=[]);}//返回結果是一個按照第幾周組成的複合數組。
getWeekOfDate方法來自 http://stackoverflow.com/questions/9045868/javascript-date-getweek
function getWeekOfDate (dateObj,dowOffset) { dowOffset = typeof(dowOffset) == 'int' ? dowOffset : 0; //default dowOffset to zero var newYear = new Date(dateObj.getFullYear(),0,1); var day = newYear.getDay() - dowOffset; //the day of week the year begins on day = (day >= 0 ? day : day + 7); var daynum = Math.floor((dateObj.getTime() - newYear.getTime() - (dateObj.getTimezoneOffset()-newYear.getTimezoneOffset())*60000)/86400000) + 1; var weeknum; //if the year starts before the middle of a week if(day < 4) { weeknum = Math.floor((daynum+day-1)/7) + 1; if(weeknum > 52) { nYear = new Date(dateObj.getFullYear() + 1,0,1); nday = nYear.getDay() - dowOffset; nday = nday >= 0 ? nday : nday + 7; /*if the next year starts before the middle of the week, it is week #1 of that year*/ weeknum = nday < 4 ? 1 : 53; } } else { weeknum = Math.floor((daynum+day-1)/7); } return weeknum;};
1)計算日期對應的周資訊
function getWeekOfYear(date){ var year=date.getFullYear(); var baseDate=new Date(year,0,1); var baseWeekDay=testDate.getDay(); var dateWeekDay=date.getDay(); if(baseWeekDay==0) baseWeekDay=7; if(dateWeekDay==0) dateWeekDay=7; var weekOfYear=Math.round(((date.getTime()-dateWeekDay*3600*24*1000)-(baseDate.getTime()-baseWeekDay*3600*24*1000))/(24*3600*1000)/7); return weekOfYear+1;}
2)將在同一周的資料相加
var dateArray=["2011-1-1","2011-1-2","2011-1-3","2014-10-01",'2014-01-01','2015-11-11'];var dateTpArray=[9,8,7,6,121323445,23492742742374324];var dateReg=/^(\d{4})\-(\d{1,2})\-(\d{1,2})$/;var reduceResult={};dateArray.forEach(function(item,index){ var matchResult=dateReg.exec(item); console.log('%s %s %s',matchResult[1],matchResult[2],matchResult[3]); var weekOfYear=getWeekOfYear(new Date(parseInt(matchResult[1],10),parseInt(matchResult[2],10)-1,parseInt(matchResult[3],10))); if(!reduceResult[matchResult[1]+'_'+weekOfYear]){ reduceResult[matchResult[1]+'_'+weekOfYear]={ desc:matchResult[1]+'第'+weekOfYear+'周', tps:0 } } reduceResult[matchResult[1]+'_'+weekOfYear].tps=reduceResult[matchResult[1]+'_'+weekOfYear].tps+dateTpArray[index];});var weekOfYearDescArray=[];var weekofYearTpArray=[];for(var prop in reduceResult){ if(reduceResult.hasOwnProperty((prop))){ weekOfYearDescArray.push(reduceResult[prop].desc); weekofYearTpArray.push(reduceResult[prop].tps); }}console.log(weekOfYearDescArray);console.log(weekofYearTpArray);