最近项目需要计算两个时间戳相差的时间并返回具体的时间.上代码
//封装计算距离奖励截止函数
getRateEndTime(rateTime, newTime) {
var tmp_time = newTime;
var days = rateTime;
var time = days - tmp_time;
let day = parseInt(time / 3600.0 / 24);
let hour = parseInt(
(parseFloat(time / 3600.0 / 24) - parseInt(time / 3600.0 / 24)) * 24
);
let min = parseInt(
(parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60
);
let sec = time % 60;
hour = hour < 10 ? "0" + hour : hour;
min = min < 10 ? "0" + min : min;
sec = sec < 10 ? "0" + sec : sec;
//console.log(time);
if (time < 0) {
return "已结束";
}
if (time < 86400) {
return `${hour} : ${min} : ${sec}`;
}
if (time > 0) {
return `${day}天 ${hour} : ${min} : ${sec}`;
}
}
newTime 是当前的时间戳.
rateTime 是后台返回需要与当前时间戳比较的.
86400是一天的毫秒数.