2 回答
TA贡献1860条经验 获得超9个赞
function setYearObject(yearStart, yearEnd) {
const obj = {};
const start = new Date(yearStart, 0);
const end = new Date(yearEnd, 0);
const months = {
Jan: [], Feb: [], Mar: [], Apr: [], May: [], Jun: [],
Jul: [], Aug: [], Sep: [], Oct: [], Nov: [], Dec: []
};
while (yearStart < yearEnd) { obj[yearStart++] = months }
while (start < end) {
const timeStamp = start.getTime()
const parts = start.toDateString().split(" ");
const year = start.getFullYear().toString()
obj[year][parts[1]].push({ day: parts[0], timeStamp: timeStamp });
start.setDate(start.getDate() + 1);
}
return obj;
}
console.log(setYearObject(2017, 2020))
TA贡献1829条经验 获得超13个赞
我不知道这是否是实现这一目标的好方法......但无论如何这就是我现在拥有的方式。
setYearObject(yearStart, yearEnd) {
let obj = {};
let start = new Date(yearStart,0);
let currentYear = yearStart;
let currentDay = start;
let week = [];
while(currentYear <= yearEnd){
obj[currentYear] = [];
currentYear++;
}
currentYear = yearStart;
console.log(currentDay.getDate());
while(currentYear !== parseInt(yearEnd) +1){
week[currentDay.getDay()] = {day: currentDay.getDate(), month:currentDay.getMonth(), dayOfWeek: this.getWeekday(currentDay.getDay())}
if(currentDay.getDay() >= 6){
obj[currentYear].push(week);
week = [];
}
currentDay.setDate(currentDay.getDate() + 1);
currentYear = currentDay.getFullYear();
}
for (var property in obj) {
if (obj.hasOwnProperty(property)) {
let tmpYear = parseInt(property);
if(obj[property].length <=52 && obj[property].length >= 1){
// obj[(tmpYear)].push(obj[(tmpYear + 1)][0])
console.log(obj[tmpYear + 1])
if( obj[tmpYear + 1] !== undefined){
if((obj[tmpYear +1]).length >= 1){
obj[tmpYear].push(obj[tmpYear + 1 ][0]);
}
}
}
}
}
console.log(obj);
return obj;
}
添加回答
举报