首先要获取到2个时间点,格式是yyyy-MM-dd HH:mm:ss
startTime是开始的时间(后台返回的),endTime是结束的时间(我自己获取的当前时间)
首先对获取的当前时间进行处理
let date = new Date() let year = date.getFullYear() < 10 ? "0" + date.getFullYear() : date.getFullYear() let month = date.getMonth() < 10 ? "0" + (parseInt(date.getMonth()) +1) :(parseInt( date.getMonth())+1) let day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate() // console.log(date.getMonth()) let all = year + "-" + month + "-" + day console.log(all) let hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours() let minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes() let second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds() let dates = hour + ":" + minute + ":" + second console.log(dates) let query = all + " " + dates比较时间的方法
compareTime(startTime,endTime) { console.log(startTime,endTime) var startTimes = startTime.substring(0, 10).split('-'); var endTimes = endTime.substring(0, 10).split('-'); startTime = startTimes[1] + '-' + startTimes[2] + '-' + startTimes[0] + ' ' + startTime.substring(10, 19); endTime = endTimes[1] + '-' + endTimes[2] + '-' + endTimes[0] + ' ' + endTime.substring(10, 19); var resultTime = (Date.parse(endTime) - Date.parse(startTime)) / 3600 / 1000; if (resultTime < 0) { console.log("endTime小于tartTime!"); } else if (resultTime > 0) { console.log("endTime大于tartTime!"); } else if (resultTime == 0) { console.log("endTime等于tartTime!"); } else { console.log('计算时间异常') } },调用:
//userInfo.levelEndTime这个时间是后台返回给我的,格式和上面的一样 compareTim(userInfo.levelEndTime,query)