先封装个办法:
/**
* @description: 获取以后工夫和进行推算后的工夫(stime, etime)
* @param {number} period - 向前推算的工夫(s)
* @return {*}
*/
handleTimeString(period) {
let etime, stime, timeString, newStime
// 以后工夫:格局 YYYY-MM-DD HH:MM:SS
etime = new Date().toLocaleDateString().replace(/\//g, "-") + "" + new Date().toTimeString().split(" ")[0]
// 将以后工夫包装成规范工夫
timeString = new Date(etime)
// 将以秒 (s) 为单位进行演算后的工夫包装成规范工夫
stime = new Date(timeString.setTime(timeString.getTime() - period))
// 演算后的工夫:格局 YYYY-MM-DD HH:MM:SS
newStime = stime.toLocaleDateString().replace(/\//g, "-") + "" + stime.toTimeString().split(" ")[0]
// 以对象的模式返回
return {
stime: newStime,
etime: etime,
}
},
实操:
切换工夫:全副转换成秒
switch (timeLimit) {let timeObj = {}
case "10min":
timeObj = this.handleTimeString(10 * 60 * 1000)
break
case "30min":
timeObj = this.handleTimeString(30 * 60 * 1000)
break
case "1h":
timeObj = this.handleTimeString(60 * 60 * 1000)
break
case "12h":
timeObj = this.handleTimeString(12 * 60 * 60 * 1000)
break
case "1d":
timeObj = this.handleTimeString(24 * 60 * 60 * 1000)
break
case "3d":
timeObj = this.handleTimeString(3 * 24 * 60 * 60 * 1000)
break
}
成果:
Keep foolish, keep hungry.