天天看點

微信小程式,計算時分秒時間差

shijiancha: function (faultDate, completeTime) {

var stime = Date.parse(new Date(faultDate));

var etime = Date.parse(new Date(completeTime));

var usedTime = etime - stime; //兩個時間戳相差的毫秒數

var days = Math.floor(usedTime / (24 * 3600 * 1000));

//計算出小時數

var leave1 = usedTime % (24 * 3600 * 1000); //計算天數後剩餘的毫秒數

var hours = Math.floor(leave1 / (3600 * 1000));

//計算相差分鐘數

var leave2 = leave1 % (3600 * 1000); //計算小時數後剩餘的毫秒數

var minutes = Math.floor(leave2 / (60 * 1000));

 

var dayStr = days == 0 ? "" : days + "天";

var hoursStr = hours == 0 ? "" : hours + "時";

 

var time = dayStr + hoursStr + minutes + "分";

return time;

},
           
onLoad: function(options) {

let aa ="2019-05-22 16:42:59";

let bb = "2019-05-22 18:34:59";

console.log(page.shijiancha(aa, bb));

}