根据拿到的长整型,UI上需要表示出一台Server已经启动了多少时间,比如1年1个月4天 14:22:38
以下是我的代码:
function deltaTimeFormatter(ms){
if(!ms)
return "N/A";
var totalS, totalMin, totalH, d, h, min, s;
totalS = Math.floor(ms / 1000);
totalMin = Math.floor(totalS / 60);
s = String(totalS % 60);
totalH = Math.floor(totalMin / 60);
min = String(totalMin % 60);
d = Math.floor(totalH / 24);
h = String(totalH % 24);
let str = `${h.padStart(2, "0")}:${min.padStart(2, "0")}:${s.padStart(2, "0")}`;
str = (d ? d + "days, " : "") + str;
return str ;
}
目前我只能计算到 xx天 hh:mm:ss,年月日感觉要看大小月份,闰年等,有什么办法计算吗?
推荐moment.js。我记得是有diff函数计算两个时间点之差的