# javascript算法题

0

## 4个回答

0
``````function timediff(a, b) {
var delta = Math.round(Math.abs(a - b) / (1000 * 60));// 相差分钟数
var day = Math.floor(delta / (60 * 24)); // 天
delta %= (60 * 24);
var hour = Math.floor(delta / 60); // 小时
delta %= 60;
var minute = delta; // 分钟

return [day, '天', hour, '小时', minute, '分钟'].join('');
}

function timediff2(a, b) {
var fragments = [];
var delta = Math.round(Math.abs(a - b) / (1000 * 60));// 相差分钟数
var day = Math.floor(delta / (60 * 24)); // 天
delta %= (60 * 24);
if (day > 0) {
fragments.push(day, '天');
}
var hour = Math.floor(delta / 60); // 小时
delta %= 60;
if (hour > 0) {
fragments.push(hour, '小时');
}

var minute = delta; // 分钟
fragments.push(minute, '分钟');

return fragments.join('');
}``````
0

0

diff = Math.abs(a - b);
now = new Date();
time = new Date(now.getTime() + diff);
diffDays = time.getDate() - now.getDate();
diffHours = time.getHours() - now.getHours();
diffMinutes = time.getMinutes() - now.getMinutes();

0
``````function timeInterval(a, b) {
var result = null;
var differMin = Math.floor(Math.abs(a - b) / 1000 / 60);
var differHour = Math.floor(differMin / 60);

var day = Math.floor(differHour / 24);
var hour = differHour - day * 24;
var minute = differMin - differHour * 60;

result = day + "天" + hour + "小时" + minute + "分钟";
return result;
}``````