如何实现readhub小程序端今天昨天的展示?

{id: "42", title: "2018年体育中考内容出炉", date: "20180205", create_time: "1517646345"}

{id: "41", title: "全域实施新建住宅建筑全", date: "20180205", create_time: "1517630772"}

{id: "40", title: "高校毕业生就业集中招动", date: "20180204", create_time: "1517207532"}

{id: "39", title: "92号汽油每升上涨5分钱", date: "20180204", create_time: "1517112928"}

列表输出可自由定制,需要参数,可加


如何用js实现如下,今天、昨天的分组

https://img.mukewang.com/5c909ff80001c58b05000444.jpg

POPMUISE
浏览 670回答 2
2回答

LEATH

先用js计算出今天的时间,把时间格式弄成跟数据里面date格式一致,今天、昨天都可以通过js计算出来吧,把这个日期传入数组,查询出date里面与当前日期匹配的数据,然后push成一个新数组,渲染到页面就行了。代码就不贴了吧,实际上就一个计算日期和通过日期查询数据的过程。

慕桂英3389331

你的数据应该是按照时间顺序的,先计算出今天昨天前天一周前你需要的分组时间,然后遍历数组比较var data = data;var flagn = 0;var flagb1 = 0;var flagb3 = 0;var flagb7 = 0;var flagb9 = 0;for (var i = 0; i < data.length; i++) {&nbsp; &nbsp; data[i].date = null;&nbsp; &nbsp; var secondTime = data[i].time;&nbsp; &nbsp; //获取 0:0 的时间戳&nbsp; &nbsp; var nowDate = new Date(new Date().setHours(0, 0, 0, 0));&nbsp; &nbsp; var before1Date = new Date(new Date(new Date().setDate(new Date().getDate() - 1)).setHours(0, 0, 0, 0));&nbsp; &nbsp; var before3Date = new Date(new Date(new Date().setDate(new Date().getDate() - 3)).setHours(0, 0, 0, 0));&nbsp; &nbsp; var before7Date = new Date(new Date(new Date().setDate(new Date().getDate() - 7)).setHours(0, 0, 0, 0));&nbsp; &nbsp; if (secondTime >= nowDate) {&nbsp; &nbsp; &nbsp; if (flagn == 0) {&nbsp; //第一个就加入一个标志&nbsp; 用来在渲染的时候判断显示今天&nbsp; &nbsp; &nbsp; &nbsp; data[i].date = '今天';&nbsp; &nbsp; &nbsp; &nbsp; flagn = 1;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } else if (secondTime >= before1Date) {&nbsp; &nbsp; &nbsp; if (flagb1 == 0) {&nbsp; &nbsp; &nbsp; &nbsp; data[i].date = '昨天';&nbsp; &nbsp; &nbsp; &nbsp; flagb1 = 1;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } else if (secondTime >= before3Date) {&nbsp; &nbsp; &nbsp; if (flagb3 == 0) {&nbsp; &nbsp; &nbsp; &nbsp; data[i].date = '三天前';&nbsp; &nbsp; &nbsp; &nbsp; flagb3 = 1;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } else if (secondTime >= before7Date) {&nbsp; &nbsp; &nbsp; if (flagb7 == 0) {&nbsp; &nbsp; &nbsp; &nbsp; data[i].date = '一周前';&nbsp; &nbsp; &nbsp; &nbsp; flagb7 = 1;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; if (flagb9 == 0) {&nbsp; &nbsp; &nbsp; &nbsp; data[i].date = '很久以前';&nbsp; &nbsp; &nbsp; &nbsp; flagb9 = 1;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp;}&nbsp;return data;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript