已知两个时间戳a,b,单位ms
把它们的差值转换成 x天x小时x分钟
如何设计该算法,可以将误差降至最低呢?
diff = Math.abs(a - b);
now = new Date();
time = new Date(now.getTime() + diff);
diffDays = time.getDate() - now.getDate();
diffHours = time.getHours() - now.getHours();
diffMinutes = time.getMinutes() - now.getMinutes();
function timeInterval(a, b) {
var result = null;
var differMin = Math.floor(Math.abs(a - b) / 1000 / 60);
var differHour = Math.floor(differMin / 60);
var day = Math.floor(differHour / 24);
var hour = differHour - day * 24;
var minute = differMin - differHour * 60;
result = day + "天" + hour + "小时" + minute + "分钟";
return result;
}
10 回答11.3k 阅读
5 回答4.9k 阅读✓ 已解决
4 回答3.2k 阅读✓ 已解决
2 回答2.8k 阅读✓ 已解决
3 回答2.4k 阅读✓ 已解决
3 回答2.2k 阅读✓ 已解决
2 回答2.7k 阅读✓ 已解决