queryTime:function(pintime){ var start_date = new Date(); var end_date = new Date(pintime.replace(/-/g, "https://www.jb51.net/")); var days = end_date.getTime() - start_date.getTime(); console.log("获取到时间差" + days) return days; }
之前困惑我的地方就是如何将时间差这个数组添加到原来的数组中,现在想想思路清晰,曾经的难题也不过尔尔。
index.js方法二修改后的代码
Page({ /** * 页面的初始数据 */ data: { pingData: [ { "id": "1", "icon": "../../images/image2.jpg", "number": "20", "pingTime": "2019-4-15 23:30:00", "time": "55267", "showList": "false", }, { "id": "2", "icon": "../../images/image3.jpg", "number": "4566", "pingTime": "2019-4-13 12:30:00", "time": "58934", "showList": "false", }, { "id": "3", "icon": "../../images/image2.jpg", "number": "20", "pingTime": "2019-4-13 08:30:00", "time": "555234", "showList": "false", } ], time:"60" }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { var that = this var pinData = that.data.pingData for (var i = 0; i < pinData.length;i++){ console.log("计算出长度为" + pinData.length) var endtime = that.data.pingData[i].pingTime console.log("计算出长度为" + endtime) that.queryTime(endtime) var time ="pingData["+i+"].time" that.setData({ [time]:that.queryTime(endtime), listData:pinData }) } that.setCountDown(); that.setTimeCount(); }, /** * 60s倒计时 */ setTimeCount:function(){ let time=this.data.time time--; if (time <= 0) { time = 0; } this.setData({ time:time }) setTimeout(this.setTimeCount,1000); }, /** * 倒计时 */ setCountDown: function () { let time = 1000; let { listData } = this.data; let list = listData.map((v, i) => { if (v.time <= 0) { v.time = 0; } let formatTime = this.getFormat(v.time); v.time -= time; v.countDown = `${formatTime.hh}:${formatTime.mm}:${formatTime.ss}`; return v; }) this.setData({ listData: list }); setTimeout(this.setCountDown, time); }, /** * 格式化时间 */ getFormat: function (msec) { let ss = parseInt(msec / 1000); let ms = parseInt(msec % 1000); let mm = 0; let hh = 0; if (ss > 60) { mm = parseInt(ss / 60); ss = parseInt(ss % 60); if (mm > 60) { hh = parseInt(mm / 60); mm = parseInt(mm % 60); } } ss = ss > 9 ? ss : `0${ss}`; mm = mm > 9 ? mm : `0${mm}`; hh = hh > 9 ? hh : `0${hh}`; return { ss, mm, hh }; }, queryTime:function(pintime){ var start_date = new Date(); var end_date = new Date(pintime.replace(/-/g, "https://www.jb51.net/")); var days = end_date.getTime() - start_date.getTime(); console.log("获取到时间差" + days) return days; } })