JS计算两个时间相差的天数、小时数、分钟数和秒数

function timeAgo(timeA, timeB) {

	var stamp = new Date(timeA).getTime() - new Date(timeB).getTime();
	var time = (stamp / 1000);

	if(null != time && "" != time) {
		if(time > 60 && time < 60 * 60) {
			time = parseInt(time / 60.0) + "分钟";
		} else if(time >= 60 * 60 && time < 60 * 60 * 24) {
			time = parseInt(time / 3600.0) + "小时" + parseInt((parseFloat(time / 3600.0) -
				parseInt(time / 3600.0)) * 60) + "分钟";
		} else if(time >= 60 * 60 * 24) {
			time = parseInt(time / 3600.0 / 24) + "天" + parseInt((parseFloat(time / 3600.0 / 24) -
				parseInt(time / 3600.0 / 24)) * 24) + "小时" + parseInt((parseFloat(time / 3600.0) -
				parseInt(time / 3600.0)) * 60) + "分钟";
		} else {
			time = parseInt(time) + "秒";
		}
	}
	return time;
}

调用:

timeAgo("2020-04-02 00:00:00", "2020-03-30 16:08:00");

返回:

"2天7小时52分钟1秒"

- END -

你可能感兴趣的:(Js/Jq,javascript,timeAgo,计算天时分秒)