首页 > 代码库 > 计算两个日期时间之间的时间差:28小时38分钟

计算两个日期时间之间的时间差:28小时38分钟

<script src="http://www.mamicode.com/moment.min.js"></script>

var now = "04/09/2017 13:59:59";
var then = "02/09/2017 14:20:30";
var ms = moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"));
var d = moment.duration(ms);
var s = Math.floor(d.asHours()) + moment.utc(ms).format(":mm:ss");
console.log(s)
// outputs: "48:39:30"

var then = moment().format(‘YYYY-MM-DD HH:mm:ss‘);
setInterval(function(){
var now = moment().format(‘YYYY-MM-DD HH:mm:ss‘);
var ms = moment(now,"YYYY-MM-DD HH:mm:ss").diff(moment(then,"YYYY-MM-DD HH:mm:ss"));
var d = moment.duration(ms);
var h = Math.floor(d.asHours());
var m = moment.utc(ms).format("mm");
$("p").html(h+‘小时‘+m+‘分钟‘);
},60000)
// outputs: "0小时00分钟"

setInterval(function(){
var now = moment().format(‘YYYY-MM-DD HH:mm:ss‘);
// console.log(then)
// console.log(now)
var ms = moment(now,"YYYY-MM-DD HH:mm:ss").diff(moment(then,"YYYY-MM-DD HH:mm:ss"));
var d = moment.duration(ms);
var h = Math.floor(d.asHours());
var m = moment.utc(ms).format("mm");
var s = moment.utc(ms).format("ss");
// var s = Math.floor(d.asHours()) + moment.utc(ms).format(":mm:ss");
$("div").html(s+‘秒‘);
},1000)
// outputs: "01秒"

链接:http://codego.net/6837891/

计算两个日期时间之间的时间差:28小时38分钟