function timeFormat(newTimeStr, oldTimeStr){ newTimeStr = newTimeStr.replace(/-/g, "/"); oldTimeStr = oldTimeStr.replace(/-/g, "/"); var newTime = new Date(newTimeStr).getTime(); var oldTime = new Date(oldTimeStr).getTime(); var diffTime = newTime - oldTime; if(diffTime < 0){ return "请确保参数顺序"; } var hh = Math.floor(diffTime / 60 / 60 / 1000); hh = hh < 10 ? "0" + hh : hh; diffTime -= hh * 60 * 60 * 1000; var mm = Math.floor(diffTime / 60 / 1000); mm = mm < 10 ? "0" + mm : mm; diffTime -= mm * 60 * 1000; var ss = Math.floor(diffTime / 1000); ss = ss < 10 ? "0" + ss : ss; return hh + ":" + mm + ":" + ss; } timeFormat("2018-05-11 15:09:09", "2018-05-11 12:10:10");