以 json 格式获得整整 3 年

用 3 整年创建 json 对象的最佳方法是什么。


例如:


{

    2017:{

        jan:[

            {day:'monday', timestamp:12512421},

            {day:'tuseday', timestamp:151512}

        ]...

    },

    2018:{

        jan:[

                {day:'monday', timestamp:12512421},

                {day:'tuseday', timestamp:151512}

        ]...

    }

    2019:{

        jan:[

            {day:'monday', timestamp:12512421},

            {day:'tuseday', timestamp:151512}

        ]...

    }

}

我需要能够显示未来一周的。例如:2019 年第 8 周的日期是什么。我需要知道从哪里开始制定 json 对象……或者它是否已经存在于某种库中。


但我不确定从什么开始开始尝试。


富国沪深
浏览 138回答 2
2回答

慕码人2483693

function setYearObject(yearStart, yearEnd) {&nbsp; &nbsp; const obj = {};&nbsp; &nbsp; const start = new Date(yearStart, 0);&nbsp; &nbsp; const end = new Date(yearEnd, 0);&nbsp; &nbsp; const months = {&nbsp; &nbsp; &nbsp; Jan: [], Feb: [], Mar: [], Apr: [], May: [], Jun: [],&nbsp; &nbsp; &nbsp; &nbsp; Jul: [], Aug: [], Sep: [], Oct: [], Nov: [], Dec: []&nbsp; &nbsp; };&nbsp;&nbsp; &nbsp; while (yearStart < yearEnd) { obj[yearStart++] = months&nbsp; }&nbsp; &nbsp; while (start < end) {&nbsp; &nbsp; &nbsp; &nbsp; const timeStamp = start.getTime()&nbsp; &nbsp; &nbsp; &nbsp; const parts = start.toDateString().split(" ");&nbsp; &nbsp; &nbsp; &nbsp; const year = start.getFullYear().toString()&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; obj[year][parts[1]].push({ day: parts[0], timeStamp: timeStamp });&nbsp; &nbsp; &nbsp; &nbsp; start.setDate(start.getDate() + 1);&nbsp; &nbsp; }&nbsp; &nbsp; return obj;}console.log(setYearObject(2017, 2020))

烙印99

我不知道这是否是实现这一目标的好方法......但无论如何这就是我现在拥有的方式。setYearObject(yearStart, yearEnd) {&nbsp; &nbsp; let obj = {};&nbsp; &nbsp; let start = new Date(yearStart,0);&nbsp; &nbsp; let currentYear = yearStart;&nbsp; &nbsp; let currentDay = start;&nbsp; &nbsp; let week = [];&nbsp; &nbsp; while(currentYear <= yearEnd){&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; obj[currentYear] = [];&nbsp; &nbsp; &nbsp; &nbsp; currentYear++;&nbsp; &nbsp; }&nbsp; &nbsp; currentYear = yearStart;&nbsp; &nbsp; console.log(currentDay.getDate());&nbsp; &nbsp; while(currentYear !== parseInt(yearEnd) +1){&nbsp; &nbsp; &nbsp; &nbsp; week[currentDay.getDay()] = {day: currentDay.getDate(), month:currentDay.getMonth(), dayOfWeek: this.getWeekday(currentDay.getDay())}&nbsp; &nbsp; &nbsp; &nbsp; if(currentDay.getDay() >= 6){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj[currentYear].push(week);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; week = [];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; currentDay.setDate(currentDay.getDate() + 1);&nbsp; &nbsp; &nbsp; &nbsp; currentYear = currentDay.getFullYear();&nbsp; &nbsp; }&nbsp; &nbsp; for (var property in obj) {&nbsp; &nbsp; &nbsp; &nbsp; if (obj.hasOwnProperty(property)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let tmpYear = parseInt(property);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(obj[property].length <=52 && obj[property].length >= 1){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // obj[(tmpYear)].push(obj[(tmpYear + 1)][0])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(obj[tmpYear + 1])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( obj[tmpYear + 1] !== undefined){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if((obj[tmpYear +1]).length >= 1){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj[tmpYear].push(obj[tmpYear + 1 ][0]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; console.log(obj);&nbsp; &nbsp; return obj;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript