时间:2021-07-01 10:21:17 帮助过:14人阅读
["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);