支持传入格式
- new Date() 形式
- 字符串形式 '2020/11/30 '或 '2020-11-30'
- 字符串形式 '2020-11-20T12:00:29.609859+08:00'
- 数字格式的时间戳 1623736698905
代码
function timeDifference(start, end) {
var date1 = new Date(start);
var date2 = new Date(end);
var date3 = date2.getTime() - date1.getTime();
var s = Math.floor(date3/1000);
var m = Math.floor(s/60);
var h = Math.floor(m/60);
var days = Math.floor(date3/(24*3600*1000));
var leave1 = date3%(24*3600*1000);
var hours = Math.floor(leave1/(3600*1000));
var leave2 = leave1%(3600*1000);
var minutes = Math.floor(leave2/(60*1000));
var leave3 = leave2%(60*1000);
var seconds = Math.round(leave3/1000);
console.log('大约分别有:' + s + '秒!', m+'分!', h + '小时!', days + '天!');
console.log('两者相差:' + days + '天 ' + hours + '小时 ' + minutes + '分钟 ' + seconds+'秒');
}
timeDifference('2020-11-20T12:00:29.609859+08:00', '2020-11-23T15:04:38.263337+08:00');
发表评论: