后台接口返回的13为时间戳
需要与数据data的time做对比,查找出最近的值
data的数据结构如下:
将:改为空格,变成数字之间的对比
//查找最近的时间
getNearestTime(timestamp, data) {
let date = new Date(timestamp)
let h = date.getHours()
let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes())
let hm = parseInt(h + '' + m)
let nearestTime = ''
let distance = 1000000
for (let i in data) {
let time = parseInt(data[i].time.replace(':', ''))
let dis = Math.abs(hm - time)
if (dis < distance) {
distance = dis
nearestTime = data[i].time
}
}
return nearestTime
},
let nearestWakeupTimeStart = this.getNearestTime(wakeupTimeStart, this.heartListData)
infos(timestamp, data){
let date = new Date(timestamp)
let h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
let m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
let targetDate = h + '' + m
var closestDate = data.reduce((prev, curr) => {
var prevDiff = Math.abs(targetDate - prev.time.replace(':', ''));
var currDiff = Math.abs(targetDate - curr.time.replace(':', ''));
return currDiff < prevDiff ? curr : prev;
});
return closestDate.time
},
let nearestWakeupTimeStarts = this.infos(wakeupTimeStart, this.heartListData)