javascript - 计算流逝时间

将时间转换为中文展示,格式为:x天x小时x分x秒

/**
 * 计算流逝时间
 * @param time 时间
 * @param trim 是否去除0值
 */
Utils.prototype.elapsedTime = function (time, trim) {
	if (trim === undefined) {
		trim = true;
	}
	if (time <= 0) {
		time = -time;
	}
	var d = Math.round(time / 86400000);
	time %= 86400000;
	var h = Math.round(time / 3600000);
	time %= 3600000;
	var m = Math.round(time / 60000);
	time %= 60000;
	var s = Math.round(time / 1000);
	var sb = '';
	if (!trim || d !== 0)
		sb = sb + d + "天";
	if (!trim || h !== 0)
		sb = sb + h + "小时";
	if (!trim || m !== 0)
		sb = sb + m + "分";
	if (!trim || s !== 0)
		sb = sb + s + "秒";
	return sb;
};

posted on 2020-05-06 21:26  疯狂的妞妞  阅读(253)  评论(0编辑  收藏  举报

导航